14

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.

mKorbel
  • 109,525
  • 20
  • 134
  • 319
Harshveer Singh
  • 4,037
  • 7
  • 37
  • 45

2 Answers2

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
  • 1
    Whoa! 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
6

JTable can listnening for selected TableCell (by mouseclick or from keyboard),you have to look for implemets ListSelectionListener, examples here or here

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319