【发布时间】:2012-01-23 14:53:10
【问题描述】:
在执行ListSelectionListener 的valueChanged 方法期间,有什么方法可以判断用户是否点击了JList 或setSelectedIndex 是否被其他代码调用?
【问题讨论】:
-
摇摆?如果是,也将摇摆添加到标签中
-
没办法。如果您觉得需要区分,通常视图和数据之间的连接逻辑不够丰富
在执行ListSelectionListener 的valueChanged 方法期间,有什么方法可以判断用户是否点击了JList 或setSelectedIndex 是否被其他代码调用?
【问题讨论】:
AFAIK,不。但是,如果目标是仅在选择来自用户的情况下执行某些操作,您可以在选择代码中的索引之前设置一个标志以表明选择不是来自用户,或者删除侦听器并在之后添加它:
private void selectIndexInList(int index) {
this.selectionComesFromTheCode = true;
try {
list.setSelectedIndex(index);
}
finally {
this.selectionComesFromTheCode = false;
}
}
@Override
public void valueChanged(ListSelectionEvent e) {
if (!this.selectionComesFromTheCode) {
...
}
}
【讨论】: