jdk/src/share/classes/javax/swing/ListCellRenderer.java
author malenkov
Wed, 07 May 2008 23:20:32 +0400
changeset 466 6acd5ec503a8
parent 2 90ce3da70b43
child 4378 ef21a120cb18
permissions -rw-r--r--
4935607: RFE: LTP: Should be possible to set the TRANSIENT attribute of propertiies to FALSE Summary: Add the Transient annotation and support it (JSR-273) Reviewed-by: peterz, loneid
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
     2
 * Copyright 1997-2005 Sun Microsystems, Inc.  All Rights Reserved.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Sun designates this
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * by Sun in the LICENSE file that accompanied this code.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    21
 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 * CA 95054 USA or visit www.sun.com if you need additional information or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
 * have any questions.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package javax.swing;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.awt.Component;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 * Identifies components that can be used as "rubber stamps" to paint
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * the cells in a JList.  For example, to use a JLabel as a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * ListCellRenderer, you would write something like this:
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * class MyCellRenderer extends JLabel implements ListCellRenderer {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 *     public MyCellRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *         setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *     public Component getListCellRendererComponent(JList list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 *                                                   Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *                                                   int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *                                                   boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *                                                   boolean cellHasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *         setText(value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *         Color background;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *         Color foreground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *         // check if this cell represents the current DnD drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *         JList.DropLocation dropLocation = list.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *         if (dropLocation != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *                 && !dropLocation.isInsert()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *                 && dropLocation.getIndex() == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *             background = Color.BLUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *             foreground = Color.WHITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *         // check if this cell is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *         } else if (isSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *             background = Color.RED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *             foreground = Color.WHITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *         // unselected, and not the DnD drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *             background = Color.WHITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *             foreground = Color.BLACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *         };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *         setBackground(background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *         setForeground(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *         return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 * }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * @see JList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 * @see DefaultListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
public interface ListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
     * Return a component that has been configured to display the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
     * value. That component's <code>paint</code> method is then called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
     * "render" the cell.  If it is necessary to compute the dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
     * of a list because the list cells do not have a fixed size, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * is called to generate a component on which <code>getPreferredSize</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * can be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * @param list The JList we're painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * @param value The value returned by list.getModel().getElementAt(index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * @param index The cells index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * @param isSelected True if the specified cell was selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param cellHasFocus True if the specified cell has the focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @return A component whose paint() method will render the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @see JList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @see ListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @see ListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    Component getListCellRendererComponent(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
        JList list,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
        boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
        boolean cellHasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
}