J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 1997-2005 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any 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 | * class MyCellRenderer extends JLabel implements ListCellRenderer { |
| 37 | * public MyCellRenderer() { |
| 38 | * setOpaque(true); |
| 39 | * } |
| 40 | * |
| 41 | * public Component getListCellRendererComponent(JList list, |
| 42 | * Object value, |
| 43 | * int index, |
| 44 | * boolean isSelected, |
| 45 | * boolean cellHasFocus) { |
| 46 | * |
| 47 | * setText(value.toString()); |
| 48 | * |
| 49 | * Color background; |
| 50 | * Color foreground; |
| 51 | * |
| 52 | * // check if this cell represents the current DnD drop location |
| 53 | * JList.DropLocation dropLocation = list.getDropLocation(); |
| 54 | * if (dropLocation != null |
| 55 | * && !dropLocation.isInsert() |
| 56 | * && dropLocation.getIndex() == index) { |
| 57 | * |
| 58 | * background = Color.BLUE; |
| 59 | * foreground = Color.WHITE; |
| 60 | * |
| 61 | * // check if this cell is selected |
| 62 | * } else if (isSelected) { |
| 63 | * background = Color.RED; |
| 64 | * foreground = Color.WHITE; |
| 65 | * |
| 66 | * // unselected, and not the DnD drop location |
| 67 | * } else { |
| 68 | * background = Color.WHITE; |
| 69 | * foreground = Color.BLACK; |
| 70 | * }; |
| 71 | * |
| 72 | * setBackground(background); |
| 73 | * setForeground(foreground); |
| 74 | * |
| 75 | * return this; |
| 76 | * } |
| 77 | * } |
| 78 | * </pre> |
| 79 | * |
| 80 | * @see JList |
| 81 | * @see DefaultListCellRenderer |
| 82 | * |
| 83 | * @author Hans Muller |
| 84 | */ |
| 85 | public interface ListCellRenderer |
| 86 | { |
| 87 | /** |
| 88 | * Return a component that has been configured to display the specified |
| 89 | * value. That component's <code>paint</code> method is then called to |
| 90 | * "render" the cell. If it is necessary to compute the dimensions |
| 91 | * of a list because the list cells do not have a fixed size, this method |
| 92 | * is called to generate a component on which <code>getPreferredSize</code> |
| 93 | * can be invoked. |
| 94 | * |
| 95 | * @param list The JList we're painting. |
| 96 | * @param value The value returned by list.getModel().getElementAt(index). |
| 97 | * @param index The cells index. |
| 98 | * @param isSelected True if the specified cell was selected. |
| 99 | * @param cellHasFocus True if the specified cell has the focus. |
| 100 | * @return A component whose paint() method will render the specified value. |
| 101 | * |
| 102 | * @see JList |
| 103 | * @see ListSelectionModel |
| 104 | * @see ListModel |
| 105 | */ |
| 106 | Component getListCellRendererComponent( |
| 107 | JList list, |
| 108 | Object value, |
| 109 | int index, |
| 110 | boolean isSelected, |
| 111 | boolean cellHasFocus); |
| 112 | } |