tags:

views:

19

answers:

1

How to get the line number or cell number by double-clicking the mouse in the table.

+1  A: 

This isn't the clearest question, but I'm going to assume:

  1. You're talking about JTables
  2. You're asking for the row index
  3. You want to output the row index to stdout

You can add a MouseListener to a JTable that fires on mouse events, and implement the mouseClicked method. The MouseEvent passed to the mouseClicked method has getButton to determine if it was a left click, and getClickCount to determine if it was a double click. If so, the JTable has getSelectedRow to determine the selected row index

It'll look something like:

final JTable table;
// ...
table.addMouseListener(new MouseAdapter() {
    @Override public void mouseClicked(MouseEvent e) {
        if(e.getButton() == MouseEvent.BUTTON1 && e.getClickCount() == 2)
            System.out.println("Current row index: " + table.getSelectedRow());
    }
});
Michael Mrozek