java JTable 单击行侦听器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17632410/
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 clicked row listener
提问by giozh
I have a dynamic JTable
that contains a string matrix, and I need to write a listener that when double click on a row, read a specific column and make some computation on it. Which kind of listener should I use?
我有一个JTable
包含字符串矩阵的动态,我需要编写一个监听器,当双击一行时,读取特定列并对其进行一些计算。我应该使用哪种监听器?
回答by Juned Ahsan
Implement the MouseListener
or extend the MouseAdapter
. You can try something like this:
实施MouseListener
或扩展MouseAdapter
. 你可以尝试这样的事情:
yourJTable.addMouseListener(new MouseAdapter(){
public void mouseClicked(MouseEvent evnt) {
if (evnt.getClickCount() == 1) {
getPropertyFromRow((String)(t_property.getValueAt(yourJTable.getSelectedRow(),0)));
}
}
});
回答by NINCOMPOOP
Try using the getClickCount()of MouseEvent
method after implementing the MouseListener
or extending the MouseAdapter
. Sample :
尝试使用getClickCount()的MouseEvent
方法实现之后MouseListener
或延长MouseAdapter
。样本 :
yourJTable.addMouseListener(new MouseAdapter() {
public void mouseClicked(MouseEvent e) {
if (e.getClickCount() == 2) { // check if a double click
// your code here
}
}
});