001 /*
002 * Copyright 1997-2005 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package javax.swing;
027
028 import java.awt.Component;
029
030 /**
031 * Identifies components that can be used as "rubber stamps" to paint
032 * the cells in a JList. For example, to use a JLabel as a
033 * ListCellRenderer, you would write something like this:
034 * <pre>
035 * class MyCellRenderer extends JLabel implements ListCellRenderer {
036 * public MyCellRenderer() {
037 * setOpaque(true);
038 * }
039 *
040 * public Component getListCellRendererComponent(JList list,
041 * Object value,
042 * int index,
043 * boolean isSelected,
044 * boolean cellHasFocus) {
045 *
046 * setText(value.toString());
047 *
048 * Color background;
049 * Color foreground;
050 *
051 * // check if this cell represents the current DnD drop location
052 * JList.DropLocation dropLocation = list.getDropLocation();
053 * if (dropLocation != null
054 * && !dropLocation.isInsert()
055 * && dropLocation.getIndex() == index) {
056 *
057 * background = Color.BLUE;
058 * foreground = Color.WHITE;
059 *
060 * // check if this cell is selected
061 * } else if (isSelected) {
062 * background = Color.RED;
063 * foreground = Color.WHITE;
064 *
065 * // unselected, and not the DnD drop location
066 * } else {
067 * background = Color.WHITE;
068 * foreground = Color.BLACK;
069 * };
070 *
071 * setBackground(background);
072 * setForeground(foreground);
073 *
074 * return this;
075 * }
076 * }
077 * </pre>
078 *
079 * @see JList
080 * @see DefaultListCellRenderer
081 *
082 * @version 1.25 05/05/07
083 * @author Hans Muller
084 */
085 public interface ListCellRenderer {
086 /**
087 * Return a component that has been configured to display the specified
088 * value. That component's <code>paint</code> method is then called to
089 * "render" the cell. If it is necessary to compute the dimensions
090 * of a list because the list cells do not have a fixed size, this method
091 * is called to generate a component on which <code>getPreferredSize</code>
092 * can be invoked.
093 *
094 * @param list The JList we're painting.
095 * @param value The value returned by list.getModel().getElementAt(index).
096 * @param index The cells index.
097 * @param isSelected True if the specified cell was selected.
098 * @param cellHasFocus True if the specified cell has the focus.
099 * @return A component whose paint() method will render the specified value.
100 *
101 * @see JList
102 * @see ListSelectionModel
103 * @see ListModel
104 */
105 Component getListCellRendererComponent(JList list, Object value,
106 int index, boolean isSelected, boolean cellHasFocus);
107 }
|