java ListSelectionListener 调用了两次
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12975460/
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
ListSelectionListener invoked twice
提问by tadpole
class MyListListener implements ListSelectionListener {
public void valueChanged (ListSelectionEvent e) {
JList source = (JList) e.getSource();
// do something
}
}
JList myList = new JList (myModel);
myList.addListSelectionListener (new MyListListener());
I am doing something very simple. I have a JList. If an item on the list is selected, the handler is called. The problem is the handler is invoked twice when I go from one item to another. I can see the use if the first trigger passes on the original selected item, and the second trigger passes on the new item. But both times, the same new item is passed. What is the point of that ? Is there a way to prevent the handler from being called twice ?
我正在做一些非常简单的事情。我有一个 JList。如果选择了列表中的一项,则调用处理程序。问题是当我从一个项目转到另一个项目时,处理程序会被调用两次。如果第一个触发器通过原始选定项目,第二个触发器通过新项目,我可以看到使用。但两次都通过了相同的新项目。那有什么意义呢?有没有办法防止处理程序被调用两次?
回答by Robin
e.getValueIsAdjusting
is what you are looking for. As stated in the javadoc of that method
e.getValueIsAdjusting
就是你要找的。如该方法的javadoc所述
Returns whether or not this is one in a series of multiple events, where changes are still being made
返回这是否是仍在进行更改的一系列多个事件中的一个
回答by Aubin
It's the normal way, you have to filter with getValueIsAdjusting()method
这是正常方式,您必须使用getValueIsAdjusting()方法进行过滤