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