7

As you know, without using !getValueIsAdjusting when you select a row in a jtable (by clicking) the selection change event fire twice. It doesn't happen if you select a row using the keyboard arrow. To resolve it, you check if getValueIsAdjusting returns false.

My question is why does the event fire twice if I select a row by clicking it, but not when using the keyboard arrow? And what does getValueIsAdjusting do to resolve it?

Cœur
  • 37,241
  • 25
  • 195
  • 267
CodeBlue
  • 340
  • 2
  • 4
  • 9
  • 3
    Have you at least read the javadoc of the method, and the linked javadoc of setValueIsAdjusting()? What don't you understand in this documentation. I don't see how it could be clearer. http://docs.oracle.com/javase/6/docs/api/javax/swing/event/ListSelectionEvent.html#getValueIsAdjusting%28%29 – JB Nizet Jun 02 '12 at 06:54
  • Hi! "Returns true if the selection is undergoing a series of changes.". I don't understand WHY, too. – Chameleon Apr 25 '19 at 16:13

2 Answers2

13

As the javadoc which JB Nizet linked to states, getValueIsAdjusting() checks whether a specific event (a change) is part of a chain, if so it will return true. It will only return false when the specified event is the final one in the chain.

In your case, selecting a row by clicking actually fires two events: a mouseDown and mouseUp event and both are sent to your event listener. If you correctly implement getValueIsAdjusting() to return whenever the value is true, you will only act on the final event in the chain, which is the mouseUp event that fires when you let go of the left mouse button.

The Java Tutorials include an example that captures events, you can use that to log the selection events and experiment with it yourself. Remove the return on the event.getValueIsAdjusting() check to log every event that's fired.

Lilienthal
  • 4,327
  • 13
  • 52
  • 88
  • Good explanation; more examples [here](http://stackoverflow.com/a/10623134/230513) and [here](http://stackoverflow.com/a/7519403/230513). – trashgod Jun 02 '12 at 11:00
  • Thanks for your explaination. I got it clearer now. But one more thing, a chain of event, why mouseDown and mouseUp, not gainFocus and lostFocus? – CodeBlue Jun 02 '12 at 14:48
  • I think is actually related to the fact that when you click on the table it will select a cell (row and column) first and then the whole row, so there are two consecutive row selections. – jigzat Aug 14 '13 at 21:04
1
    String interessen[]= {"aaaaaaa", "bbbbbbbb", "ccccccccc", "ddddddd"};

    myList = new JList<>(interessen);

    myList.addListSelectionListener(new ListSelectionListener() {


        @Override
        public void valueChanged(ListSelectionEvent e) {
            if(!e.getValueIsAdjusting())
            System.out.println(myList.getSelectedValue());

        }
    });

The code above shows what getValueIsAdjusting do, without these method an event may be called eg. two times (it depends of event).

Output without getValueIsAdjusting loop after clicking on some element of JList: aaaaaaa aaaaaaa

with loop: aaaaaaa

Tomasz Waszczyk
  • 2,680
  • 5
  • 35
  • 73