Class CustomComboBoxRenderer

  • All Implemented Interfaces:
    javax.swing.ListCellRenderer

    public class CustomComboBoxRenderer
    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(java.lang.Object) method and class by calling JComboBox.setRenderer(javax.swing.ListCellRenderer<? super E>) on an instance of the resulting subclass.

    Author:
    Mark Taylor (Starlink)
    • 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)  
      java.lang.Object getNullRepresentation()
      Returns the representation for the null value.
      protected java.lang.Object mapValue​(java.lang.Object value)
      Turns an object which might be found in the ComboBox itself into an object that can be rendered by a standard combobox renderer.
      void setNullRepresentation​(java.lang.Object nullRep)
      Sets the representation for the null value.
      • Methods inherited from class java.lang.Object

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

      • CustomComboBoxRenderer

        public CustomComboBoxRenderer()
    • 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
      • setNullRepresentation

        public void setNullRepresentation​(java.lang.Object nullRep)
        Sets the representation for the null value. If set to a non-null value, this will be used to render a null; otherwise, mapValue(java.lang.Object) will be called as usual (which may itself do something with the null).
        Parameters:
        nullRep - null representation
      • getNullRepresentation

        public java.lang.Object getNullRepresentation()
        Returns the representation for the null value.
        Returns:
        null representation
      • mapValue

        protected java.lang.Object mapValue​(java.lang.Object value)
        Turns an object which might be found in the ComboBox itself into an object that can be rendered by a standard combobox renderer. Typically the return value of this method would be a String more suitable than the result of value's toString method.

        The default implementation just returns the value itself

        Parameters:
        value - value to map
        Returns:
        value to map it into (probably a string)