9

I was wondering how to modify a JList so that clicking any values would not do anything. I have looked at other questions but none have helped.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
nrubin29
  • 1,522
  • 5
  • 25
  • 53

2 Answers2

14

I solved it by using the following class:

class DisabledItemSelectionModel extends DefaultListSelectionModel {

    @Override
    public void setSelectionInterval(int index0, int index1) {
        super.setSelectionInterval(-1, -1);
    }
}

I instantiated the class here:

console.setSelectionModel(new DisabledItemSelectionModel());

nrubin29
  • 1,522
  • 5
  • 25
  • 53
  • 3
    This method still allows the user to select elements with CTRL + Mouse1 click unless selection model is set to `SINGLE_SELECTION`. – Dev Apr 27 '15 at 20:33
  • 2
    For me, setting selection mode (!) to `SINGLE_SELECTION` does not prevent it. But overriding `public void addSelectionInterval(int index0, int index1)` with the same `super.setSelectionInterval(-1, -1);` does. – Bowi Feb 03 '17 at 14:32
3

Assuming your objects in your JList are clickable items, just do setEnabled(false) on all the objects you want to disable

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
StormeHawke
  • 5,987
  • 5
  • 45
  • 73
  • 1
    @HovercraftFullOfEels I am writing Strings to the JList, so there's no way I could disable Strings. – nrubin29 Jul 25 '13 at 16:45