Java Swing - JComboBox with Icons Example

[Updated: Nov 20, 2017, Created: Nov 20, 2017]

Following example shows JCombo with a custom renderer having items with icons.

public class JComboBoxExample {
  public static void main(String[] args) {
      List<Employee> employees = EmployeeDataAccess.getEmployees();
      JComboBox<Employee> comboBox = new JComboBox<>(employees.toArray(new Employee[employees.size()]));
      //renderer
      comboBox.setRenderer(new ExampleRenderer());
      //wrap in a panel
      JPanel panel = new JPanel();
      panel.add(comboBox);
      //frame
      JFrame frame = createFrame();
      frame.add(panel);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
  }

  private static JFrame createFrame() {
      JFrame frame = new JFrame("JComboBox Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(new Dimension(600, 300));
      return frame;
  }
}
public class ExampleRenderer extends DefaultListCellRenderer {
  private Map<String, ImageIcon> iconMap = new HashMap<>();
  private Color background = new Color(0, 100, 255, 15);
  private Color defaultBackground = (Color) UIManager.get("List.background");

  public ExampleRenderer() {
      iconMap.put("Account", new ImageIcon(getClass().getResource("/images/account.png")));
      iconMap.put("Sales", new ImageIcon(getClass().getResource("/images/sales.png")));
      iconMap.put("IT", new ImageIcon(getClass().getResource("/images/it.png")));
      iconMap.put("Admin", new ImageIcon(getClass().getResource("/images/admin.png")));
  }

  @Override
  public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                                                boolean isSelected, boolean cellHasFocus) {
      super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
      Employee emp = (Employee) value;
      this.setText(emp.getName());
      this.setIcon(iconMap.get(emp.getDept()));
      if (!isSelected) {
          this.setBackground(index % 2 == 0 ? background : defaultBackground);
      }
      return this;
  }
}

The four icons loaded in above code is already in the folder src/main/resources/images/.

public class EmployeeDataAccess {

  public static List<Employee> getEmployees() {
      List<Employee> list = new ArrayList<>();
      String[] depts = {"IT", "Account", "Admin", "Sales"};
      DataFactory df = new DataFactory();
      for (int i = 1; i <= 30; i++) {
          Employee e = new Employee();
          e.setName(df.getName());
          e.setAddress(df.getAddress() + ", " + df.getCity());
          e.setDept(df.getItem(depts));
          e.setPhone(df.getNumberText(3) + "-" + df.getNumberText(3) +
                  "-" + df.getNumberText(4));
          list.add(e);
      }
      return list;
  }
}
public class Employee {
  private String name;
  private String dept;
  private String phone;
  private String address;
    .............
}

Output

Example Project

Dependencies and Technologies Used :

  • datafactory 0.8: Library to generate data for testing.
  • JDK 1.8
  • Maven 3.3.9

JComboBox With Icons Example Select All Download
  • combo-box-with-icons-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
        • resources
          • images

See Also