22

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?

Nathan
  • 8,093
  • 8
  • 50
  • 76
Cristian
  • 221
  • 1
  • 2
  • 3

5 Answers5

35

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());
    // ...
Pops
  • 30,199
  • 37
  • 136
  • 151
17

You can use following methods on JTable to retrieve row and column of the selected cell:

int rowIndex = table.getSelectedRow();
int colIndex = table.getSelectedColumn();

And add a SelectionListener to table to catch the event when the table is selected.

Georg Leber
  • 3,470
  • 5
  • 40
  • 63
  • 2
    +1, except the ListSelectionListener is added to the ListSelectionModel, not the JTable. – camickr Jan 25 '11 at 16:38
  • 6
    You also might wanna add a ListSelectionListener to the JTable's ColumnModel in case selected row stays the same but selected column changes: table.getColumnModel().getSelectionModel().addListSelectionListener(...); – Uhlen Jan 26 '11 at 20:04
5

It is working for me!!!

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) {


    }
 }
});
Tamil
  • 1,193
  • 9
  • 24
  • This solution acknowledges the possibility of clicking the empty space in the JTable component. – Queeg Jul 14 '21 at 21:52
1

I've found that when columns are hidden/reordered columnAtPoint returns the visible column index, which isn't what I needed. Code which worked for me is

int row = theTable.convertRowIndexToModel(theTable.rowAtPoint(event.getPoint()));
int col = theTable.convertColumnIndexToModel(theTable.columnAtPoint(event.getPoint()));
Alexey Romanov
  • 167,066
  • 35
  • 309
  • 487
1

did you try addMouseListener()? I hope you are about using Swing's JTable.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • yes I'm using Swing's and no, I haven't tried addMouseListener. will try right now... thanks. – Cristian Jan 25 '11 at 15:52
  • this is kinda confusing, could you give some pointers or some links? I've been searching and no result. I just want the row & column of selected jTable CELL to be added in a jLabel..... – Cristian Jan 25 '11 at 15:57
  • I don't think is a good way to tackle the issue. The MouseEvent will not be that easy to convert to a column and row. See Develman's answer. – jzd Jan 25 '11 at 16:24
  • I did not mean to add mouse listener to the table itself. Typically table cell is represented by some swing component managed by table model. I suggest to add listener to this component. – AlexR Jan 25 '11 at 16:29
  • 1
    -1, A cell is represented by painting an image of a component, called a renderer. You can't add a listener to the renderer since it is not a real component. If you where to use a MouseListener it would indeed need to be added to the table, but there is no need to do this since this is the purpose of the ListSelectionListener. – camickr Jan 25 '11 at 16:35