Class CustomComboBoxRenderer<T>

java.lang.Object
uk.ac.starlink.util.gui.CustomComboBoxRenderer<T>
All Implemented Interfaces:
ListCellRenderer<T>

public class CustomComboBoxRenderer<T> extends Object implements ListCellRenderer<T>
Utility class which does the job of rendering items into a JComboBox when you just want to provide a different stringification of them than the one provided by the toString method.

You would use this class by providing an implementation of the mapValue(T) method and class by calling JComboBox.setRenderer(javax.swing.ListCellRenderer<? super E>) on an instance of the resulting subclass.

Author:
Mark Taylor (Starlink)
  • Constructor Details

    • CustomComboBoxRenderer

      public CustomComboBoxRenderer()
      Constructs a renderer for which nulls are represented as blank.
    • CustomComboBoxRenderer

      public CustomComboBoxRenderer(String nullTxt)
      Constructs a renderer with a custom null representation.
      Parameters:
      nullTxt - text to be displayed for null values
  • Method Details

    • getListCellRendererComponent

      public Component getListCellRendererComponent(JList<? extends T> list, T value, int index, boolean isSelected, boolean hasFocus)
      Specified by:
      getListCellRendererComponent in interface ListCellRenderer<T>
    • mapValue

      protected String mapValue(T value)
      Turns a non-null object which might be found in the ComboBox itself into a string to be displayed by a standard combobox renderer. The default implementation just uses the toString method.

      This method will only be invoked if value is not null. In case of null, the nullTxt value supplied at construction tim will be used instead.

      Parameters:
      value - non-null value to map
      Returns:
      display string