通过实现ListCellRenderer接口,实现CheckBox的列表,下面是从Koders中选择的一段代码。
1 |
/* * * $Id: BasicCheckComboBoxRenderer.java,v 1.1 2004/05/20 11:13:57 dvstar Exp $ * * Copyright (C) 2003-2004 Dmitry Starjinsky * * File : BasicCheckComboBoxRenderer.java * Description : * Author's email : star@unicorn.kiev.ua * Author's Website : * * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the * Free Software Foundation, Inc., 59 Temple Place - Suite 330, * Boston, MA 02111-1307, USA. */ package unicorn.javax.swing; import javax.swing.*; import javax.swing.event.*; import javax.swing.border.*; import java.awt.*; import java.io.Serializable; /** * ComboBox renderer * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is appropriate * for short term storage or RMI between applications running the same * version of Swing. A future release of Swing will provide support for * long term persistence. * * @version 1.16 02/02/00 * @author Arnaud Weber */ public class BasicCheckComboBoxRenderer extends JCheckBox implements ListCellRenderer, Serializable { protected static Border noFocusBorder = new EmptyBorder(1, 1, 1, 1); public BasicCheckComboBoxRenderer() { super(); setOpaque(true); setBorder(noFocusBorder); } public Dimension getPreferredSize() { Dimension size; if ((this.getText() == null) || (this.getText().equals( "" ))) { setText( " " ); size = super.getPreferredSize(); setText( "" ); } else { size = super.getPreferredSize(); } return size; } public Component getListCellRendererComponent( JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) { /**if (isSelected) { setBackground(UIManager.getColor("ComboBox.selectionBackground")); setForeground(UIManager.getColor("ComboBox.selectionForeground")); } else { setBackground(UIManager.getColor("ComboBox.background")); setForeground(UIManager.getColor("ComboBox.foreground")); }**/ if (isSelected) { // setSelected(true); setBackground(list.getSelectionBackground()); setForeground(list.getSelectionForeground()); } else { setBackground(list.getBackground()); setForeground(list.getForeground()); } setFont(list.getFont()); //System.out.println(getText()); if (value instanceof JCheckBox) { setText( ((JCheckBox)value).getText() );; setSelected( ((JCheckBox)value).isSelected() ); } /* if (value instanceof Icon) { setIcon((Icon)value); } else { setText((value == null) ? "" : value.toString()); } */ // setText((value == null) ? "" : value.toString()); return this; } /** * A subclass of BasicComboBoxRenderer that implements UIResource. * BasicComboBoxRenderer doesn't implement UIResource * directly so that applications can safely override the * cellRenderer property with BasicListCellRenderer subclasses. * <p> * <strong>Warning:</strong> * Serialized objects of this class will not be compatible with * future Swing releases. The current serialization support is appropriate * for short term storage or RMI between applications running the same * version of Swing. A future release of Swing will provide support for * long term persistence. */ public static class UIResource extends BasicCheckComboBoxRenderer implements javax.swing.plaf.UIResource { } } |