I have created a table in java in Netbeans and filled it with some data. Now I want to show some detail in a text area corresponding to the particular column in a row when I click on that cell. How can I find out using event listener that on which cell user has clicked.
Asked
Active
Viewed 8.3k times
2 Answers
67
Find the location of the click event and get the cell you are searching for:
jTable1.addMouseListener(new java.awt.event.MouseAdapter() {
@Override
public void mouseClicked(java.awt.event.MouseEvent evt) {
int row = jTable1.rowAtPoint(evt.getPoint());
int col = jTable1.columnAtPoint(evt.getPoint());
if (row >= 0 && col >= 0) {
......
}
}
});

Costis Aivalis
- 13,680
- 3
- 46
- 47
-
1Whoa! Such an elegant answer! I've tried to add a MouseListener to overide item selection event but it didn't work, turn out I have to use MouseAdapter. Thank you sir. – Anh Tuan Sep 24 '14 at 10:07