java getValueIsAdjusting 究竟做了什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10860419/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:53:10  来源:igfitidea点击:

What exactly does getValueIsAdjusting do?

javaswingjtable

提问by CodeBlue

As you know, without using !getValueIsAdjustingwhen 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 getValueIsAdjustingreturns false.

如您所知,不使用!getValueIsAdjusting当您在 jtable 中选择一行(通过单击)时,选择更改事件会触发两次。如果您使用键盘箭头选择一行,则不会发生这种情况。要解决它,请检查是否getValueIsAdjusting返回 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?

我的问题是为什么如果我通过单击选择一行,事件会触发两次,但在使用键盘箭头时不会触发?getValueIsAdjusting 如何解决它?

回答by Lilienthal

As the javadocwhich 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 falsewhen the specified event is the final one in the chain.

正如JB Nizet 链接到的javadoc所述,getValueIsAdjusting()检查特定事件(更改)是否是链的一部分,如果是,它将返回true. 只有false当指定的事件是链中的最后一个时,它才会返回。

In your case, selecting a row by clicking actually fires two events: a mouseDownand mouseUpevent 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 mouseUpevent that fires when you let go of the left mouse button.

在您的情况下,通过单击选择一行实际上会触发两个事件: amouseDownmouseUpevent 并且两者都发送到您的事件侦听器。如果您正确地实现getValueIsAdjusting()在值为 时返回true,您将只对链中的最后一个事件采取行动,即mouseUp当您松开鼠标左键时触发的事件。

The Java Tutorials include an examplethat 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.

Java 教程包含一个捕获事件的示例,您可以使用它来记录选择事件并自己进行试验。删除event.getValueIsAdjusting()检查的返回以记录触发的每个事件。

回答by Tomasz Waszczyk

    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).

上面的代码显示了 getValueIsAdjusting 的作用,如果没有这些方法,则可以调用事件,例如。两次(这取决于事件)。

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

单击 JList 的某个元素后没有 getValueIsAdjusting 循环的输出:aaaaaaa aaaaaaa

with loop: aaaaaaa

带循环:aaaaaaa