Class ComboBoxListRenderer<T>

Type Parameters:
T - the type of elements in the list
All Implemented Interfaces:
ImageObserver, MenuContainer, Serializable, Accessible, ListCellRenderer<T>, SwingConstants

public final class ComboBoxListRenderer<T> extends JLabel implements ListCellRenderer<T>
A custom list cell renderer for JComboBox components that provides improved visibility.

This renderer addresses issues with the Substance Look and Feel where selected items have black foreground text on dark backgrounds, making them difficult to read. This implementation ensures selected items are displayed with white text and adds padding for better readability.

Features:

  • White text for selected items on dark gray background
  • Light gray text for unselected items
  • Automatic title case formatting for enum values
  • Consistent padding around list items

Usage:

 JComboBox<MyEnum> comboBox = new JComboBox<>(MyEnum.values());
 comboBox.setRenderer(new ComboBoxListRenderer<>());
 

See Also:
  • Constructor Details

    • ComboBoxListRenderer

      public ComboBoxListRenderer()
  • Method Details

    • getListCellRendererComponent

      public Component getListCellRendererComponent(JList<? extends T> list, T o, int index, boolean isSelected, boolean cellHasFocus)
      Returns a component configured to display the specified value.

      This method is called by the JComboBox to render each item in the dropdown list. It configures colors, borders, and text based on the item's selection state.

      Specified by:
      getListCellRendererComponent in interface ListCellRenderer<T>
      Parameters:
      list - the JList being rendered
      o - the value to render
      index - the cell index
      isSelected - true if the cell is selected
      cellHasFocus - true if the cell has focus
      Returns:
      this component configured for the specified value