FileDocCategorySizeDatePackage
DefaultListCellRenderer.javaAPI DocJava SE 6 API9258Tue Jun 10 00:26:36 BST 2008javax.swing

DefaultListCellRenderer

public class DefaultListCellRenderer extends JLabel implements Serializable, ListCellRenderer
Renders an item in a list.

Implementation Note: This class overrides invalidate, validate, revalidate, repaint, isOpaque, and firePropertyChange solely to improve performance. If not overridden, these frequently called methods would execute code paths that are unnecessary for the default list cell renderer. If you write your own renderer, take care to weigh the benefits and drawbacks of overriding these methods.

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeansTM has been added to the java.beans package. Please see {@link java.beans.XMLEncoder}.

version
1.32 11/30/05
author
Philip Milne
author
Hans Muller

Fields Summary
protected static Border
noFocusBorder
An empty Border. This field might not be used. To change the Border used by this renderer override the getListCellRendererComponent method and set the border of the returned component directly.
private static final Border
SAFE_NO_FOCUS_BORDER
Constructors Summary
public DefaultListCellRenderer()
Constructs a default renderer object for an item in a list.

    
                    
      
	super();
	setOpaque(true);
        setBorder(getNoFocusBorder());
    
Methods Summary
protected voidfirePropertyChange(java.lang.String propertyName, java.lang.Object oldValue, java.lang.Object newValue)
Overridden for performance reasons. See the Implementation Note for more information.

	// Strings get interned...
	if (propertyName == "text"
                || ((propertyName == "font" || propertyName == "foreground")
                    && oldValue != newValue
                    && getClientProperty(javax.swing.plaf.basic.BasicHTML.propertyKey) != null)) {

	    super.firePropertyChange(propertyName, oldValue, newValue);
        }
    
public voidfirePropertyChange(java.lang.String propertyName, byte oldValue, byte newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, char oldValue, char newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, short oldValue, short newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, int oldValue, int newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, long oldValue, long newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, float oldValue, float newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, double oldValue, double newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public voidfirePropertyChange(java.lang.String propertyName, boolean oldValue, boolean newValue)
Overridden for performance reasons. See the Implementation Note for more information.

public java.awt.ComponentgetListCellRendererComponent(javax.swing.JList list, java.lang.Object value, int index, boolean isSelected, boolean cellHasFocus)

        setComponentOrientation(list.getComponentOrientation());

        Color bg = null;
        Color fg = null;

        JList.DropLocation dropLocation = list.getDropLocation();
        if (dropLocation != null
                && !dropLocation.isInsert()
                && dropLocation.getIndex() == index) {

            bg = UIManager.getColor("List.dropCellBackground");
            fg = UIManager.getColor("List.dropCellForeground");

            isSelected = true;
        }

	if (isSelected) {
            setBackground(bg == null ? list.getSelectionBackground() : bg);
	    setForeground(fg == null ? list.getSelectionForeground() : fg);
	}
	else {
	    setBackground(list.getBackground());
	    setForeground(list.getForeground());
	}

	if (value instanceof Icon) {
	    setIcon((Icon)value);
	    setText("");
	}
	else {
	    setIcon(null);
	    setText((value == null) ? "" : value.toString());
	}

	setEnabled(list.isEnabled());
	setFont(list.getFont());

        Border border = null;
        if (cellHasFocus) {
            if (isSelected) {
                border = UIManager.getBorder("List.focusSelectedCellHighlightBorder");
            }
            if (border == null) {
                border = UIManager.getBorder("List.focusCellHighlightBorder");
            }
        } else {
            border = getNoFocusBorder();
        }
	setBorder(border);

	return this;
    
private static javax.swing.border.BordergetNoFocusBorder()

        if (System.getSecurityManager() != null) {
            return SAFE_NO_FOCUS_BORDER;
        } else {
            return noFocusBorder;
        }
    
public voidinvalidate()
Overridden for performance reasons. See the Implementation Note for more information.

since
1.5

public booleanisOpaque()
Overridden for performance reasons. See the Implementation Note for more information.

since
1.5
return
true if the background is completely opaque and differs from the JList's background; false otherwise

 
	Color back = getBackground();
	Component p = getParent(); 
	if (p != null) { 
	    p = p.getParent(); 
	}
	// p should now be the JList. 
	boolean colorMatch = (back != null) && (p != null) && 
	    back.equals(p.getBackground()) && 
			p.isOpaque();
	return !colorMatch && super.isOpaque(); 
    
public voidrepaint(java.awt.Rectangle r)
Overridden for performance reasons. See the Implementation Note for more information.

public voidrepaint()
Overridden for performance reasons. See the Implementation Note for more information.

since
1.5

public voidrepaint(long tm, int x, int y, int width, int height)
Overridden for performance reasons. See the Implementation Note for more information.

public voidrevalidate()
Overridden for performance reasons. See the Implementation Note for more information.

public voidvalidate()
Overridden for performance reasons. See the Implementation Note for more information.