swing 中如何将CheckBox加入ComboBox

import java.awt.*;  
   
public class CheckCombo implements ActionListener  
{  
    public void actionPerformed(ActionEvent e)  
    {  
        JComboBox cb = (JComboBox)e.getSource();  
        CheckComboStore store = (CheckComboStore)cb.getSelectedItem();  
        CheckComboRenderer ccr = (CheckComboRenderer)cb.getRenderer();  
        ccr.checkBox.setSelected((store.state = !store.state));  
    }  
   
    private JPanel getContent()  
    {  
        String[] ids = { "north", "west", "south", "east" };  
        Boolean[] values =  
        {  
            Boolean.TRUE, Boolean.FALSE, Boolean.FALSE, Boolean.FALSE  
        };  
        CheckComboStore[] stores = new CheckComboStore[ids.length];  
        for(int j = 0; j < ids.length; j++)  
            stores[j] = new CheckComboStore(ids[j], values[j]);  
        JComboBox combo = new JComboBox(stores);  
        combo.setRenderer(new CheckComboRenderer());  
        combo.addActionListener(this);  
        JPanel panel = new JPanel();  
        panel.add(combo);  
        return panel;  
    }  
   
    public static void main(String[] args)  
    {  
        JFrame f = new JFrame();  
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
        f.getContentPane().add(new CheckCombo().getContent());  
        f.setSize(300,160);  
        f.setLocation(200,200);  
        f.setVisible(true);  
    }  
}  
   
/** adapted from comment section of ListCellRenderer api */  
class CheckComboRenderer implements ListCellRenderer  
{  
    JCheckBox checkBox;  
   
    public CheckComboRenderer()  
    {  
        checkBox = new JCheckBox();  
    }  
    @Override
	public Component getListCellRendererComponent(JList list,  
                                                  Object value,  
                                                  int index,  
                                                  boolean isSelected,  
                                                  boolean cellHasFocus)  
    {  
        CheckComboStore store = (CheckComboStore)value;  
        checkBox.setText(store.id);  
        checkBox.setSelected(((Boolean)store.state).booleanValue());  
        checkBox.setBackground(isSelected ? Color.red : Color.white);  
        checkBox.setForeground(isSelected ? Color.white : Color.black);  
        return checkBox;  
    }  
}  
   
class CheckComboStore  
{  
    String id;  
    Boolean state;  
   
    public CheckComboStore(String id, Boolean state)  
    {  
        this.id = id;  
        this.state = state;  
    }  
}  

其实关键就是combo.setRenderer(new CheckComboRenderer()); 
自己定义renderer就好了。

下图是运行的结果:

refs:

http://www.coderanch.com/t/343024/GUI/java/Components-JComboBox

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值