java – ListSelectionListener被调用两次
作者:互联网
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());
我做的很简单.我有一个JList.如果选择了列表中的项,则调用该处理程序.问题是当我从一个项目转到另一个项目时,处理程序被调用两次.如果第一个触发器传递原始选定项目,我可以看到使用,第二个触发器传递新项目.但是两次都传递了相同的新项目.那是什么意思?有没有办法阻止处理程序被调用两次?
解决方法:
e.getValueIsAdjusting
正是您要找的.正如该方法的javadoc中所述
Returns whether or not this is one in a series of multiple events, where changes are still being made
标签:jlist,listselectionlistener,java,swing 来源: https://codeday.me/bug/20191007/1866356.html