通过实现ListCellRenderer接口,实现CheckBox的列表

 

通过实现ListCellRenderer接口,实现CheckBox的列表,下面是从Koders中选择的一段代码。

 

 


/* 
 *
 *  $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 {
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JBListSwing中的一个组件,它可以用来展示列表数据。默认情况下,JBList中的每一项都是不可编辑的。如果你想要让某一列可编辑,可以使用自定义的ListCellRendererListCellEditor来实现。 首先,你需要创建一个自定义的ListCellRenderer来渲染列表项。这个Renderer需要继承JLabel,并实现getListCellRendererComponent()方法。在该方法中,你需要根据列的可编辑状态来设置标签的编辑状态。例如: ```java public class MyListCellRenderer extends JLabel implements ListCellRenderer<String> { @Override public Component getListCellRendererComponent(JList<? extends String> list, String value, int index, boolean isSelected, boolean cellHasFocus) { if (list.getModel().getElementAt(index).isEditable()) { // 如果该列可编辑 setEditable(true); setOpaque(true); setBackground(isSelected ? list.getSelectionBackground() : list.getBackground()); setForeground(isSelected ? list.getSelectionForeground() : list.getForeground()); } else { setEditable(false); setOpaque(false); setBackground(list.getBackground()); setForeground(list.getForeground()); } setText(value); return this; } } ``` 接下来,你需要创建一个自定义的ListCellEditor来实现编辑功能。这个Editor需要继承DefaultListCellEditor,并实现getTableCellEditorComponent()方法。在该方法中,你需要根据列的可编辑状态来设置编辑框的编辑状态。例如: ```java public class MyListCellEditor extends DefaultListCellEditor { public MyListCellEditor() { super(new JTextField()); } @Override public Component getListCellEditorComponent(JList<? extends String> list, Object value, int index, boolean isSelected) { JTextField editor = (JTextField) super.getListCellEditorComponent(list, value, index, isSelected); if (list.getModel().getElementAt(index).isEditable()) { // 如果该列可编辑 editor.setEditable(true); editor.setBackground(list.getSelectionBackground()); editor.setForeground(list.getSelectionForeground()); } else { editor.setEditable(false); editor.setBackground(list.getBackground()); editor.setForeground(list.getForeground()); } return editor; } } ``` 最后,在使用JBList时,你需要将自定义的ListCellRendererListCellEditor设置给JBList。例如: ```java JBList<String> list = new JBList<>(new String[]{"a", "b", "c"}); list.setCellRenderer(new MyListCellRenderer()); list.setCellEditor(new MyListCellEditor()); ``` 这样,你就可以在JBList实现可编辑列了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值