12

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.

I think it would be more intuitive just to allow the user to click the items on and off without the need to hold an additional key.

Gordon
  • 4,823
  • 4
  • 38
  • 55
  • My application is aimed at the non-computer literate so I felt I didn't needed to use the standard (famous last words). I have found from experience that people sometimes forget to hold ctrl and deselect all the other items. I was going to do a bit of usability testing to see what people prefer. – Gordon Mar 08 '10 at 21:16
  • Recipe for a funky non-standard app (like Lotus Notes).. – Steve Kuo Mar 08 '10 at 21:19
  • Definitely makes sense. I was testing and there's no doc/tutorial saying that you MUST press Control to do a multiple selection... was testing and testing and found no problem in code.... +1. – WesternGun Aug 25 '16 at 09:18

3 Answers3

14

Think twice before changing default behavior. Unless you have some special use-case, I'd not like my List to work different than everywhere else :)

Having said that, you should be able to use your own ListSelectionModel:

list.setSelectionModel(new DefaultListSelectionModel() {
    @Override
    public void setSelectionInterval(int index0, int index1) {
        if(super.isSelectedIndex(index0)) {
            super.removeSelectionInterval(index0, index1);
        }
        else {
            super.addSelectionInterval(index0, index1);
        }
    }
});
Peter Lang
  • 54,264
  • 27
  • 148
  • 161
  • Thanks that worked a treat. I am not convinced it's the right thing to do but I would like test it out with a few people. – Gordon Mar 08 '10 at 21:20
  • It is working but pay attention that any ListSelectionListener on that list will not be notified. To fix that add `fireValueChanged(index0, index1);` at the end of the method. – Alex Dec 12 '14 at 13:54
  • Works fine normally, but NOT if you move the mouse one pixel while the button is pressed down. It then gets deselected again :-( – runholen Nov 10 '15 at 08:46
4
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);
        }
    }
});
Francisco
  • 41
  • 2
0

I think you can easily accomplish this by attaching a mouse listener on your JList and selecting programatically the item in the listener code. Of course you will probably need some code to determine which item was pressed basing on some coordinates.

pajton
  • 15,828
  • 8
  • 54
  • 65