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()));
Related videos on Youtube
Author by
Cristian
Updated on July 09, 2022Comments
-
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 aJLabel
? -
Cristian over 13 yearsyes I'm using Swing's and no, I haven't tried addMouseListener. will try right now... thanks.
-
Cristian over 13 yearsthis 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 over 13 yearsI 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 over 13 yearsI 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 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 over 13 years+1, except the ListSelectionListener is added to the ListSelectionModel, not the JTable.
-
Uhlen over 13 yearsYou 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(...);
-
Jesus over 8 yearsProps for this answer. getSelectedRow() or getSelectedColumn() don't work when using right click.
-
Felype over 6 yearsLike (almost) all of Java Swing "solutions", sorcks: Sucks, but works.
-
Hiran Chaudhuri almost 3 yearsThis solution acknowledges the possibility of clicking the empty space in the JTable component.