Close

Java Swing - JList with ListCellRenderer and ListSelectionListener Example

[Last Updated: Nov 20, 2017]

This example shows basic use of JList with a custom renderer and with a selection listener.

public class JListExample {
  public static void main(String[] args) {
      List<Employee> employees = EmployeeDataAccess.getEmployees();
      JList<Employee> jList = new JList<>(employees.toArray(new Employee[employees.size()]));
      jList.setCellRenderer(createListRenderer());
      jList.addListSelectionListener(createListSelectionListener(jList));
      JScrollPane pane = new JScrollPane(jList);

      JFrame frame = createFrame();
      frame.add(pane);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
  }

  private static ListSelectionListener createListSelectionListener(JList list) {
      return e -> {
          if (!e.getValueIsAdjusting()) {
              System.out.println(list.getSelectedValue());
          }
      };
  }

  private static ListCellRenderer<? super Employee> createListRenderer() {
      return new DefaultListCellRenderer() {
          private Color background = new Color(0, 100, 255, 15);
          private Color defaultBackground = (Color) UIManager.get("List.background");

          @Override
          public Component getListCellRendererComponent(JList<?> list, Object value, int index,
                                                        boolean isSelected, boolean cellHasFocus) {
              Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
              if (c instanceof JLabel) {
                  JLabel label = (JLabel) c;
                  Employee emp = (Employee) value;
                  label.setText(String.format("%s [%s]", emp.getName(), emp.getDept()));
                  if (!isSelected) {
                      label.setBackground(index % 2 == 0 ? background : defaultBackground);
                  }
              }
              return c;
          }
      };
  }

  private static JFrame createFrame() {
      JFrame frame = new JFrame("JList Example");
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(new Dimension(600, 300));
      return frame;
  }
}
public class Employee {
  private String name;
  private String dept;
  private String phone;
  private String address;
    .............
}

Output

On item selection following is printed on console window:

Employee{name='Jeffrey Suarez', dept='Admin', phone='058-795-5965', address='917 Hedgewood Square, Bowens Mill'}

Example Project

Dependencies and Technologies Used:

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

JList with Renderer and Selection Listener Example Select All Download
  • list-example
    • src
      • main
        • java
          • com
            • logicbig
              • example
                • JListExample.java

    See Also