JCheckBox组件可以添加到JComboBoxes中吗?

发布于 2024-10-16 00:13:52 字数 47 浏览 4 评论 0原文

JCheckBox组件可以添加到JComboBoxes中吗?如果是这样,怎么办?

Can JCheckBox components be added to JComboBoxes? If so, how?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

紫瑟鸿黎 2024-10-23 00:13:52

有多种方法可以将 JCheckBoxes 合并到 JComboBoxes 中,但它并不像在 JTable 中使用它们那么简单,因为 JComboBoxes 不使用单元格编辑器,只使用渲染器。让我们看看我是否可以在 Google 上重新找到此内容...

请检查此处:JComboCheckBox
在这里:组合框中的复选框

There are ways to jimmy JCheckBoxes into a JComboBoxes, but it's not nearly as straightforward as using them in a JTable since JComboBoxes don't use cell editors, only renderers. Let's see if I can re-find this on Google....

Please check here: JComboCheckBox
and here: Check Boxes in a Combobox

紫南 2024-10-23 00:13:52
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" };
        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();
    }
    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;
    }
}
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" };
        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();
    }
    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;
    }
}
情场扛把子 2024-10-23 00:13:52

您可以通过使用 Japura-GUI 库来实现此目的,该库提供了一个 CheckComboBox 组件创建带有复选框的 JComboBox。
此处找到 Japura-GUI CheckComboBox 的详细文档和使用示例

您可以在 按照文档将 CheckComboBox 集成到您的 Java Swing 应用程序中。

You can achieve this by using the Japura-GUI library, which provides a CheckComboBox component for creating JComboBoxes with checkboxes.
You can find detailed documentation and usage examples for the Japura-GUI CheckComboBox here

Simply follow the documentation to integrate the CheckComboBox into your Java Swing application.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文