Java JTable 选择更改事件处理:动态查找源表
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/375265/
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
JTable selection change event handling: find the source table dynamically
提问by MrG
I've implemented my own event handler and added it to the selection model of the table:
我已经实现了自己的事件处理程序并将其添加到表的选择模型中:
table.getSelectionModel().addListSelectionListener(event);
And implemented the method for "event" (mentioned above):
并实现了“事件”的方法(如上所述):
public void valueChanged(ListSelectionEvent e) {
log.debug("value changed");
}
Unfortunately the event fires twice if I chance the selection and it doesn't seem possible to find the associated table, because e.getSource provides javax.swing.DefaultListSelectionModel.
不幸的是,如果我有机会进行选择,则事件会触发两次,并且似乎无法找到关联的表,因为 e.getSource 提供了 javax.swing.DefaultListSelectionModel。
Hence my questions are:
因此我的问题是:
1) Why does it fire twice although the eventListener is only registered once?
1) 尽管 eventListener 只注册了一次,但为什么它会触发两次?
2) How can I find the table for which the selection applies? The DefaultListSelectionModel doesn't seem to offer any getSource() or similar.
2) 如何找到选择适用的表格?DefaultListSelectionModel 似乎没有提供任何 getSource() 或类似的。
Many thanks!
非常感谢!
采纳答案by Draemon
1) I think you'll find it fires once for de-selecting the old selection and once for selecting the new selection. If you log the details of the event you should see exactly what's going on. I can't remember the details, so perhaps this is wrong. Either way you should be able to call getValueIsAdjusting() on the event and only use the last one in the chain (ie when it returns false).
1)我想你会发现它一次取消选择旧选择,一次选择新选择。如果您记录事件的详细信息,您应该确切地看到发生了什么。我不记得细节了,所以这可能是错误的。无论哪种方式,您都应该能够在事件上调用 getValueIsAdjusting() 并且只使用链中的最后一个(即当它返回 false 时)。
2) You shouldn't normally need to, but AFAIK the only way to do this is to create your Listener specifically for the table (ie pass the table to the constructor and remember it).
2)您通常不需要,但AFAIK唯一的方法是专门为表创建监听器(即将表传递给构造函数并记住它)。
回答by Dave Ray
Since more than one JTable (or other component I'm guessing) can share the same selection model, it doesn't make sense to ask for the associated JTable from the event. This is the same reason that you can't retrieve a JTable from a TableModel. As Draemon suggests, store the reference to the JTable in (or make it accessible to) your listener class.
由于多个 JTable(或我猜的其他组件)可以共享相同的选择模型,因此从事件中请求关联的 JTable 没有意义。这与无法从 TableModel 检索 JTable 的原因相同。正如 Draemon 所建议的,将对 JTable 的引用存储在(或使其可访问)您的侦听器类中。
回答by Think Force
Thanks Draemon..It Works fine....
谢谢 Draemon ......它工作正常......
Our Code
我们的准则
vMachinesTable.getSelectionModel().addListSelectionListener(new ListSelectionListener() {
public void valueChanged(ListSelectionEvent lse) {
if (!lse.getValueIsAdjusting()) {
System.out.println("Selection Changed");
}
}
});
Thanks By
致谢
TF Team
特遣队