java 当通过鼠标更改值时,JList 会触发 valueChanged 两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12461627/
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
JList fires valueChanged twice when a value is changed via mouse
提问by TGP1994
I recently encountered a bug in java where JList will fire the valueChanged() method twice when changing a value with the mouse, and only once when changing a value with the keyboard. I just found a bugregarding this on Oracle's website (apparently, the bug is more than twelve years old), and I'm wondering if anyone can explain to me why Oracle has decided that this isn't a defect (not to mention that getValueIsAdjusting() returns false when the keyboard is used).
我最近在 java 中遇到了一个错误,其中 JList 在使用鼠标更改值时会触发 valueChanged() 方法两次,而在使用键盘更改值时只会触发一次。我刚刚在 Oracle 的网站上发现了一个关于这个的错误(显然,这个错误已经超过12 年了),我想知道是否有人可以向我解释为什么 Oracle 决定这不是一个缺陷(更不用说使用键盘时 getValueIsAdjusting() 返回 false)。
For anyone having this issue, I found that simply checking for when getValueIsAdjusting()
is false, then running the rest of my method will get around the issue.
对于遇到此问题的任何人,我发现只需检查 whengetValueIsAdjusting()
为 false,然后运行我的其余方法即可解决此问题。
回答by Mikle Garin
There is a simple explanation.
When you are applying selection with mouse you perform a list of actions:
有一个简单的解释。
当您使用鼠标应用选择时,您将执行一系列操作:
1. Press left mouse button on some element
- list selects an element under the mouse and fires 1st event
- also here you will get getValueIsAdjusting=true since the mouse is not yet released
1. 在某个元素上按下鼠标左键
- 列表选择鼠标下的一个元素并触发第一个事件
- 在这里你也会得到 getValueIsAdjusting=true 因为鼠标尚未释放
2. You might drag mouse without releasing it to change selection
- list will fire an additional event for each selection change made
- getValueIsAdjusting will be also true for each of those events since you are still making changes
2. 您可以在不释放鼠标的情况下拖动鼠标来更改选择
- 列表将为所做的每个选择更改触发一个额外的事件
- getValueIsAdjusting 对于每个这些事件也是如此,因为您仍在进行更改
3. You release mouse
- list will fire the final event - selection operation is finished
- getValueIsAdjusting=false now, you can do whatever you want with final selection
3.你松开鼠标
- list 将触发最终事件 - 选择操作完成
- 现在 getValueIsAdjusting=false,你可以对最终选择做任何你想做的事情
To summ up - those additional events are fired to let you completely control list behavior on selection changes (on selection change sequence to be exact). You might want to ignore the selection changes when getValueIsAdjusting=true since there always will be a final event with getValueIsAdjusting=false which will inform you that selection changes are finished.
总而言之 - 这些附加事件被触发,让您完全控制选择更改时的列表行为(准确地说是选择更改序列)。您可能希望在 getValueIsAdjusting=true 时忽略选择更改,因为总会有一个带有 getValueIsAdjusting=false 的最终事件,它会通知您选择更改已完成。
Also, when you change selection with key buttons list wouldn't know if you are going to change it after first key press or not, so getValueIsAdjusting will be always false for such changes.
此外,当您使用按键列表更改选择时,不知道您是否要在第一次按键后更改它,因此对于此类更改,getValueIsAdjusting 将始终为 false。
回答by Eaglechrome
There is a simple solution:
有一个简单的解决方案:
private void jList1 ValueChanged(javax.swing.event.ListSelectionEvent evt) {
if (!evt.getValueIsAdjusting()) {//This line prevents double events
}
}