2012-03-13 116 views
0

enter image description here添加列的ActionListener JTable中

大家好..我 又需要一些帮助。 :)

如何做到这一点?当我点击列t1时,必须弹出另一个表单来解释列t1发生了什么,例如,在时间1时,指令1处于获取阶段。然后,当我点击naman t2列时,指令2处于提取阶段,指令1处于解码阶段,等等。

预先感谢您。我真的需要你的帮助.. 问候.. :)

回答

2

您需要添加以下代码块,这将上双击打开

table.addMouseListener(new MouseAdapter() { 
     public void mouseClicked(MouseEvent e) { 
      // This is for double click event on anywhere on JTable 
      if (e.getClickCount() == 2) { 
       JTable target = (JTable) e.getSource(); 
       int row = target.getSelectedRow(); 
       int column = target.getSelectedColumn(); 
       // you can play more here to get that cell value and all 
       new DialogYouWantToOpen(row, Column); 
      } 
     } 

    }); 

的对话框。

class DialogYouWantToOpen extends JDialog{ 
     JLabel testLabel = new JLable(); 
     public DialogYouWantToOpen(int row, int column){ 
     setSize(200,200) 
     setLayout(new FlowLayout()); 
     testLabel.setText("User double clicked at row "+row+" and column "+ column); 
     add(testLabel); 
     } 

} 
+0

感谢你的代码,我得到它.. – Celine 2012-03-13 14:00:58

+0

非常欢迎你!。 – 2012-03-13 14:01:49

0

Generaly它应该是这样的

Listener listener = new Listener() { 
    public void handleEvent(Event e) { 
    TableColumn column = (TableColumn) e.widget; 
    System.out.println(column); 
    } 
}; 

你得到的柱而出事件,然后做你想做的事情是什么。

+0

不是来自这个问题太明显 - 但它的左右摇摆:-) – kleopatra 2012-03-13 15:13:22