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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 02:32:04  来源:igfitidea点击:

JTable clicked row listener

javaswingjtableawtlistener

提问by giozh

I have a dynamic JTablethat 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 MouseListeneror 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 MouseEventmethod after implementing the MouseListeneror 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
     }
   }
});