Class CustomComboBoxRenderer<T>

  • All Implemented Interfaces:
    javax.swing.ListCellRenderer

    public class CustomComboBoxRenderer<T>
    extends java.lang.Object
    implements javax.swing.ListCellRenderer
    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 Summary

      Constructors 
      Constructor Description
      CustomComboBoxRenderer​(java.lang.Class<T> clazz)
      Constructs a renderer for which nulls are represented as blank.
      CustomComboBoxRenderer​(java.lang.Class<T> clazz, java.lang.String nullTxt)
      Constructs a renderer with a custom null representation.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      java.awt.Component getListCellRendererComponent​(javax.swing.JList list, java.lang.Object value, int index, boolean isSelected, boolean hasFocus)  
      protected java.lang.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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • CustomComboBoxRenderer

        public CustomComboBoxRenderer​(java.lang.Class<T> clazz)
        Constructs a renderer for which nulls are represented as blank.
        Parameters:
        clazz - type of object to be rendered
      • CustomComboBoxRenderer

        public CustomComboBoxRenderer​(java.lang.Class<T> clazz,
                                      java.lang.String nullTxt)
        Constructs a renderer with a custom null representation.
        Parameters:
        clazz - type of object to be rendered
        nullTxt - text to be displayed for null values
    • Method Detail

      • getListCellRendererComponent

        public java.awt.Component getListCellRendererComponent​(javax.swing.JList list,
                                                               java.lang.Object value,
                                                               int index,
                                                               boolean isSelected,
                                                               boolean hasFocus)
        Specified by:
        getListCellRendererComponent in interface javax.swing.ListCellRenderer<T>
      • mapValue

        protected java.lang.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