java JList clearSelection() 问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3318824/
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 clearSelection() issue
提问by Martin
I have a problem with my two JList components.
我的两个 JList 组件有问题。
I created and placed on JFrame two JList components. I added listSelectionListeners to both of them that should unselect a selection of the other JList like so: (edit: To make as requested self-contained sample)
我在 JFrame 上创建并放置了两个 JList 组件。我向它们添加了 listSelectionListeners ,它们应该像这样取消选择其他 JList 的选择:(编辑:根据要求制作自包含示例)
public class JListIssue {
public static void main(String[] args) {
JFrame frame = new JFrame();
JPanel mainPanel = new JPanel();
frame.setContentPane(mainPanel);
final JList jList1 = new JList();
mainPanel.add(jList1);
final JList jList2 = new JList();
mainPanel.add(jList2);
// Setting up models for both JList components to display example values
jList1.setModel(new AbstractListModel() {
String[] items = {"Item 1", "Item 2", "Item 3"};
public int getSize() { return items.length; }
public Object getElementAt(int i) { return items[i]; }
});
jList2.setModel(new AbstractListModel() {
String[] items = {"Item 1", "Item 2", "Item 3"};
public int getSize() { return items.length; }
public Object getElementAt(int i) { return items[i]; }
});
// Adding listeners
jList1.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList2.clearSelection();
}
});
jList2.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
jList1.clearSelection();
}
});
frame.pack();
frame.setVisible(true);
}
}
However, when it deselects the selection of one of those two lists I have to click two times to make a new selection. Because first time I click it sort of borders the item I want to select by it doesn't really selects it (confirmed by listSelectionListener) so I have to either move my mouse while holding the left mouse button during the first selection or click second time and then it actually selects the item.
但是,当它取消选择这两个列表之一时,我必须单击两次才能进行新选择。因为我第一次点击它有点边框,我想通过它选择的项目并没有真正选择它(由 listSelectionListener 确认)所以我必须在第一次选择期间按住鼠标左键移动鼠标或单击第二次然后它实际上选择了该项目。
I find this behaviour wierd and I don't want it to behave like this. Any suggestion?
我发现这种行为很奇怪,我不希望它表现得像这样。有什么建议吗?
采纳答案by Sbodd
Your problem is that your ListSelectionListener gets notified in response to the clearSelection() call from the other list. When you make a selection on jlist1, it calls clearSelection on jlist2; if jlist2 has anything selected, this will trigger valueChanged on jlist2's selection listener, clearing the selection you just finished making on jlist1. You'll probably need to add a flag that lets the two listeners know if the other one is currently changing:
您的问题是您的 ListSelectionListener 收到通知以响应来自其他列表的 clearSelection() 调用。当您在 jlist1 上进行选择时,它会调用 jlist2 上的 clearSelection;如果 jlist2 选择了任何内容,这将在 jlist2 的选择侦听器上触发 valueChanged,清除您刚刚在 jlist1 上完成的选择。您可能需要添加一个标志,让两个侦听器知道另一个侦听器当前是否正在更改:
//member variable
boolean isChanging = false;
//later.... make this same change in both ListSelectionListeners!
jList2.addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent e) {
if (!isChanging) {
isChanging = true;
jList1.clearSelection();
isChanging = false;
}
}
});
});
回答by Nikita Ignatov
you can access the event variabl e, which can indicate if this current event is a series of multiple events getValueIsAdjusting()
您可以访问事件变量e,它可以指示当前事件是否是一系列多个事件getValueIsAdjusting()
public void valueChanged(ListSelectionEvent e) {
if (e.getValueIsAdjusting())
jList1.clearSelection();
}

