jdk/src/share/classes/javax/swing/ListCellRenderer.java
author dmarkov
Wed, 16 Apr 2014 12:51:25 +0400
changeset 24184 4da2f6ec4dab
parent 5506 202f599c92aa
child 25201 4adc75e0c4e5
permissions -rw-r--r--
8032874: ArrayIndexOutOfBoundsException in JTable while clearing data in JTable Reviewed-by: alexp, alexsch
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
     2
 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
2
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
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
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
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 4378
diff changeset
    23
 * questions.
2
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>
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
    36
 * {@code
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
    37
 * class MyCellRenderer extends JLabel implements ListCellRenderer<Object> {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 *     public MyCellRenderer() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 *         setOpaque(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
    42
 *     public Component getListCellRendererComponent(JList<?> list,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 *                                                   Object value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *                                                   int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *                                                   boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *                                                   boolean cellHasFocus) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 *         setText(value.toString());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *         Color background;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 *         Color foreground;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *         // check if this cell represents the current DnD drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
 *         JList.DropLocation dropLocation = list.getDropLocation();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
 *         if (dropLocation != null
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
 *                 && !dropLocation.isInsert()
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
 *                 && dropLocation.getIndex() == index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
 *             background = Color.BLUE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
 *             foreground = Color.WHITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
 *         // check if this cell is selected
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
 *         } else if (isSelected) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
 *             background = Color.RED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
 *             foreground = Color.WHITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 *         // unselected, and not the DnD drop location
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 *         } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 *             background = Color.WHITE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 *             foreground = Color.BLACK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
 *         };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
 *         setBackground(background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
 *         setForeground(foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
 *         return this;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
 *     }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
 * }
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
    79
 * }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
 * </pre>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
 *
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
    82
 * @param <E> the type of values this renderer can be used for
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
    83
 *
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
 * @see JList
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
 * @see DefaultListCellRenderer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
 * @author Hans Muller
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
 */
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
    89
public interface ListCellRenderer<E>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
     * Return a component that has been configured to display the specified
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * value. That component's <code>paint</code> method is then called to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * "render" the cell.  If it is necessary to compute the dimensions
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * of a list because the list cells do not have a fixed size, this method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * is called to generate a component on which <code>getPreferredSize</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * can be invoked.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * @param list The JList we're painting.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * @param value The value returned by list.getModel().getElementAt(index).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     * @param index The cells index.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
     * @param isSelected True if the specified cell was selected.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
     * @param cellHasFocus True if the specified cell has the focus.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
     * @return A component whose paint() method will render the specified value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * @see JList
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * @see ListSelectionModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     * @see ListModel
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    Component getListCellRendererComponent(
4378
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
   111
        JList<? extends E> list,
ef21a120cb18 6823603: Generics: JList
rupashka
parents: 2
diff changeset
   112
        E value,
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
        int index,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
        boolean isSelected,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        boolean cellHasFocus);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
}