Select Multiple Items In JList Without Using The Ctrl/Command Key

前端 未结 4 1308
温柔的废话
温柔的废话 2021-02-08 10:01

I am looking for a way to select multiple items within a JList by just clicking each item.

The normal way to do this is to hold the command/ctrl key and then click.

4条回答
  •  闹比i
    闹比i (楼主)
    2021-02-08 10:47

    list.setSelectionModel(new DefaultListSelectionModel() {
        private int i0 = -1;
        private int i1 = -1;
    
        public void setSelectionInterval(int index0, int index1) {
            if(i0 == index0 && i1 == index1){
                if(getValueIsAdjusting()){
                     setValueIsAdjusting(false);
                     setSelection(index0, index1);
                }
            }else{
                i0 = index0;
                i1 = index1;
                setValueIsAdjusting(false);
                setSelection(index0, index1);
            }
        }
        private void setSelection(int index0, int index1){
            if(super.isSelectedIndex(index0)) {
                super.removeSelectionInterval(index0, index1);
            }else {
                super.addSelectionInterval(index0, index1);
            }
        }
    });
    

提交回复
热议问题