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

前端 未结 4 395
隐瞒了意图╮
隐瞒了意图╮ 2020-12-10 17:00

I\'m aware of JList and JComboBox. I need the combo box drop down functionality with multiple selection functionality that JList provi

4条回答
  •  萌比男神i
    2020-12-10 17:34

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

提交回复
热议问题