Determine Which JTable Cell is Clicked

Cristian picture Cristian · Jan 25, 2011 · Viewed 42k times · Source

When a user clicks a cell on a JTable, how do I figure out the row and column of the clicked cell? How would I show this information in a JLabel?

Answer

Pops picture Pops · May 22, 2011

The existing answer works, but there is an alternate method that may work better if you're not enabling cell selection. Inside your MouseListener, do something like this:

public void mouseClicked(java.awt.event.MouseEvent event) {
    int row = theTable.rowAtPoint(event.getPoint());
    int col = theTable.columnAtPoint(event.getPoint());
    // ...