Search code examples
javaswingjtablejtableheader

Adding actionlistener of column in a jtable


enter image description here

Hi everyone.. I need some help again. :)

How to do this? When I click the column t1, another form must pop-up explaining what happens to column t1, say, at time 1, Instruction 1 is in fetch stage. Then, when I click naman t2 column, Instruction 2 is in fetch stage and Instruction 1 is in Decode stage., so on and so forth.

Thank you in advance. I really need your help.. Regards.. :)


Solution

  • You need to add following chunk of code,

        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);
                }
            }
    
        });
    

    A Dialog which will be opened on double click.

    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);
           }
    
    }