如何在具有多项选择的java swing中创建下拉列表?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2860250/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 13:47:40  来源:igfitidea点击:

How to create a drop-down list in java swing with multiple item selection?

javaswingdrop-down-menumultipleselection

提问by K_U

I'm aware of JListand JComboBox. I need the combo box drop down functionality with multiple selection functionality that JListprovides.

我知道JListJComboBox。我需要提供多选功能的组合框下拉功能JList

This is because the contents of the list are too huge to be displayed using a simple list. I also need to select multiple items, otherwise I would have been content with JComboBox.

这是因为列表的内容太大而无法使用简单的列表显示。我还需要选择多个项目,否则我会满足于JComboBox.

Any suggestions?

有什么建议?

采纳答案by Nate

When using multi-select, it's better to use a list than a combo box. As GUI metaphors go, people expect a combo box to be single select, whereas lists can be either.

使用多选时,最好使用列表而不是组合框。在 GUI 比喻中,人们期望组合框是单​​选的,而列表可以是两者之一。

the contents of the list are too huge to be displayed using a simple list

列表的内容太大,无法使用简单的列表显示

Place the JListin a JScrollPane. You can call setVisibleRowCount(int)on the JListto specify how many rows at a time should be shown.

JList放在JScrollPane. 您可以拨打setVisibleRowCount(INT)JList指定如何在一时间许多行应显示。

回答by Martijn Courteaux

You can make a custom cell renderer for the combobox and add checkboxes to that components, so you can check and uncheck them. You have to make something like this:

您可以为组合框制作自定义单元格渲染器,并向该组件添加复选框,以便您可以选中和取消选中它们。你必须做这样的事情:

public class MyComboBoxRenderer implements ListCellRenderer {

    private String[] items;
    private boolean[] selected;

    public MyComboBoxRenderer(String[] items){
         this.items = items;
         this.selected = new boolean[items.lenght];
    }

    public Component getListCellRendererComponent(JTable table, Object value,
            boolean isSelected, boolean hasFocus, int index) {
         // Create here a JLabel with the text
         // Create here a JCheckBox
         // Add them to a layoutmanager
         return this;
    }

    public void setSelected(int i, boolean flag)
    {
         this.selected[i] = flag;
    }

}

回答by trashgod

If your data has a hierarchical character, consider NetBeans' Outlinecomponent, discussed in Announcing the new Swing Tree Tableand in this answer. Here's the Current Development Versionof the API.

如果您的数据具有分层特征,请考虑 NetBeans 的Outline组件,在宣布新的 Swing 树表和本答案中讨论过。这是API的当前开发版本

回答by CLi

To achieve the described functionality, I finally decided to "abuse" the JMenuBarand add to it several JCheckBoxMenuItems. The GUI then perfectly fits the purpose (at least for me), it is just the handling of the ItemEvent that risks to become a bit annoying.

为了实现所描述的功能,我最终决定“滥用”JMenuBar并添加几个JCheckBoxMenuItems. 然后 GUI 完全符合目的(至少对我而言),只是 ItemEvent 的处理可能会变得有点烦人。

(finally there, I defined some bit logic over the items, and then may restrict myself to handling only one type of event)

(最后,我在项目上定义了一些位逻辑,然后可能会限制自己只处理一种类型的事件)