How to get the line number or cell number by double-clicking the mouse in the table.
views:
19answers:
1
+1
A:
This isn't the clearest question, but I'm going to assume:
- You're talking about
JTable
s - You're asking for the row index
- 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
2010-08-27 04:51:39