Determine Which JTable Cell is Clicked

42,998

Solution 1

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());
    // ...

Solution 2

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.

Solution 3

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


    }
 }
});

Solution 4

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

Solution 5

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()));
Share:
42,998

Related videos on Youtube

Cristian
Author by

Cristian

Updated on July 09, 2022

Comments

  • Cristian
    Cristian almost 2 years

    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?

  • Cristian
    Cristian over 13 years
    yes I'm using Swing's and no, I haven't tried addMouseListener. will try right now... thanks.
  • Cristian
    Cristian over 13 years
    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.....
  • jzd
    jzd over 13 years
    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.
  • AlexR
    AlexR over 13 years
    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.
  • camickr
    camickr over 13 years
    -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
    camickr over 13 years
    +1, except the ListSelectionListener is added to the ListSelectionModel, not the JTable.
  • Uhlen
    Uhlen over 13 years
    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().addListSelectionL‌​istener(...);
  • Jesus
    Jesus over 8 years
    Props for this answer. getSelectedRow() or getSelectedColumn() don't work when using right click.
  • Felype
    Felype over 6 years
    Like (almost) all of Java Swing "solutions", sorcks: Sucks, but works.
  • Hiran Chaudhuri
    Hiran Chaudhuri almost 3 years
    This solution acknowledges the possibility of clicking the empty space in the JTable component.