0

I made a game "Who Wants To Be A Millionaire" with jList filed. In the jList I listed the prises see this picture below

enter image description here

The game is started with the prise number 1 and increase the number if the answer was OK. If I move the mouse over the prises I can modify the prise position with the mouse click as well. This is what I want to disable. The jList need to have only for show the prises without modify with the mouse click. I also try to use disable of the jList but than all the colors are changed and I don't find where can I adjust the disabled colors.

What is the best solution for my need ?

Fire-In-D-Hole
  • 270
  • 2
  • 13
Szakos
  • 3
  • 3
  • Do you want the users not to be able to select JList cells with mouse? Or you want them not to change the values only? – STaefi Feb 24 '19 at 07:46
  • In that case is the same. User can't modify the jList values. jList must be a display for the prises only without any mouse modification. The modification do the program only. – Szakos Feb 24 '19 at 08:25
  • For the solution I use the jList events. jListMouseEntered --> index = jList.getSelectedIndex(); For the jListMouseExited and jListMousePressed and jListMouseDragged --> jList.setSelectedIndex(index); – Szakos Feb 24 '19 at 09:11
  • Please add your code (minimal) so that we can run it and understand your problem and requirement. – Prasad Karunagoda Feb 24 '19 at 10:07

1 Answers1

0

The simplest way to achieve that is to add a ListSelectionListener to restore the proper index into your JList.

Take a look at an example:

list.addListSelectionListener(new ListSelectionListener() {

    @Override
    public void valueChanged(ListSelectionEvent e) {
        list.setSelectedIndex(myIndex);

    }
});
list.setSelectedIndex(0); //0 plays no role, since listener will select myIndex

The selected index will always remain my index, no matter what.

Note: If you want to change the index later, you must change the value of myIndex variable without forgetting firing the selection listener as well. More accurately:

myIndex = 15;
list.setSelectedIndex(0); //0 plays no role, since the selection listener uses myIndex

Another way (more complex in my opinion) is to read and follow Disable JList Cell Selection Property.

George Z.
  • 6,643
  • 4
  • 27
  • 47