Make JList Values Unselectable [duplicate]
Asked Answered
A

2

11

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.

Adversative answered 25/7, 2013 at 16:33 Comment(0)
A
16

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());

Adversative answered 25/7, 2013 at 16:49 Comment(2)
This method still allows the user to select elements with CTRL + Mouse1 click unless selection model is set to SINGLE_SELECTION.Belenbelesprit
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.Tisiphone
H
3

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

Handwoven answered 25/7, 2013 at 16:41 Comment(1)
@HovercraftFullOfEels I am writing Strings to the JList, so there's no way I could disable Strings.Adversative

© 2022 - 2024 — McMap. All rights reserved.