如何在多个项目选择的java swing中创建一个下拉列表?

K_U*_*K_U 7 java swing multipleselection drop-down-menu

我知道JList并且JComboBox.我需要具有多种选择功能的组合框下拉功能JList.

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

有什么建议?

Nat*_*ate 9

使用多选时,最好使用列表而不是组合框.随着GUI隐喻的出现,人们希望组合框可以单选,而列表也可以.

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

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


Mar*_*aux 6

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

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;
    }

}
Run Code Online (Sandbox Code Playgroud)