tags:

views:

92

answers:

2

Hi, I am a new, terribly green user of Swing. I managed to create a table class using examples from java.sun tutorials, and I managed to load data dynamically into it. I want to be able to react to a click on a row by displaying a dialog box. How do I add the event Handler that will identify the selected row number?

The main function code:

public static void main(String[] args) {
    javax.swing.SwingUtilities.invokeLater(new Runnable() {

        public void run() {
            try {
                MainWindow window = new MainWindow();
                window.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                //Create and set up the content pane.
                createAndShowGUI();
                //...
            }
        }
    }
}

and

private static void createAndShowGUI() {
    //Create and set up the window.

    JFrame frame = new JFrame("Data Table");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    //Create and set up data of the content pane.
    TableClass mainTable = new TableClass(fh.getColNames(), fh.getTableContent());

    mainTable.setOpaque(true);
    frame.setContentPane(mainTable);


    //Display the window.
    frame.pack();
    frame.setVisible(true);
}

Thank you

+5  A: 
       table.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (SwingUtilities.isLeftMouseButton(e) && e.getClickCount() == 1) {
                    int selectedRowIndex = table.getSelectedRow();
                    //show your dialog with the selected row's contents
                }
            }
        });
Bozhidar Batsov
SwingUtilities.isLeftMouseButton(e) is pretty good trick +1
Xorty
This is based on the table selection to find out which cell the mouse was clicked in. This is mostly fine for left clicks, but how would you go about detecting where a right-click is? Right clicking doesn't adjust the table selection...
zigdon
+1  A: 

A couple of points.

The answer from Bozhidar Batsov is correct. However, you may not necessarily have a reference to the table (if for example your mouse listener is in another class or something). Point is, the table can be found from the MouseEvent's getSource() method. You can safely cast it to a JTable.

Also, table.getSelectedRow() may return a -1 if no row has actually been selected on the table yet. That would happen if the user, for example, clicks in the "whitespace" of the table (the area outside of the grid, but still in the text area). Just be sure to test for a -1 in your code.

taftster
Thanking all the people that gave me good ideas. I solved the problem by using more or less the code given to me by Bozhidar Batsov. I also took care of returning a -1 if no row has actually been selected on the table (warning from taftser)
Ayelet
Is there a way to mark that my question is answered and I don't need more answers? (I am not only new to Swing, also new to this site)
Ayelet
Click on the tick beside the answer you like the most and it will become green, marking it as your preferred answer and effectively solving the question.
Bozhidar Batsov