首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >选择JComboBox中的所有JComboBox

选择JComboBox中的所有JComboBox
EN

Stack Overflow用户
提问于 2015-03-11 08:01:07
回答 1查看 2K关注 0票数 0

我在JCheckBox代码中有JComboBox

代码语言:javascript
复制
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

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", "ALL" };
        Boolean[] values =
        {
            Boolean.TRUE, Boolean.FALSE, 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();
    }
    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;
    }
}

如何在选中ALL和“de”检查(False)后选中所有复选框

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-03-11 08:19:41

当您选择“所有”时,您已全部选择,当您取消选择“所有”时,您已全部取消选择。试着玩它。

代码语言:javascript
复制
public class CheckCombo implements ActionListener {

    JComboBox combo = null;

    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));
        if (store.id.equals("ALL")){
            for (int i = 0; i < combo.getItemCount(); i++){
                ((CheckComboStore)combo.getItemAt(i)).state = ccr.checkBox.isSelected();
            }
        }
    }

    private JPanel getContent() {

        String[] ids = { "north", "west", "south", "east", "ALL" };
        Boolean[] values = { Boolean.TRUE, Boolean.FALSE, 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]);
        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();
    }

    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;
    }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/28981273

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档