uk.ac.starlink.util.gui
Class CustomComboBoxRenderer

java.lang.Object
  extended by uk.ac.starlink.util.gui.CustomComboBoxRenderer
All Implemented Interfaces:
ListCellRenderer

public class CustomComboBoxRenderer
extends Object
implements 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) on an instance of the resulting subclass.

Author:
Mark Taylor (Starlink)

Constructor Summary
CustomComboBoxRenderer()
           
 
Method Summary
 Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean hasFocus)
           
 Object getNullRepresentation()
          Returns the representation for the null value.
protected  Object mapValue(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(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 Component getListCellRendererComponent(JList list,
                                              Object value,
                                              int index,
                                              boolean isSelected,
                                              boolean hasFocus)
Specified by:
getListCellRendererComponent in interface ListCellRenderer

setNullRepresentation

public void setNullRepresentation(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 Object getNullRepresentation()
Returns the representation for the null value.

Returns:
null representation

mapValue

protected Object mapValue(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)


Copyright © 2015 Central Laboratory of the Research Councils. All Rights Reserved.