Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine Which JTable Cell is Clicked

Tags:

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?

like image 782
Cristian Avatar asked Jan 25 '11 15:01

Cristian


People also ask

How do I know if a JTable row is selected?

So you can call table. getSelectionModel(). isSelectionEmpty() to find out if any row is selected.

Which of the following model determines how items are selected in JTable?

ListSelectionModel newmodel = mytable.

Is cell editable JTable?

The isCellEditable() method of JTable (or the TableModel) controls whether a cell is editable or not. By default it just return "true". So you can override the method to return a boolean value that is set by your "Modify" button.

What is JTable TableModel?

The TableModel interface specifies the methods the JTable will use to interrogate a tabular data model. The JTable can be set up to display any data model which implements the TableModel interface with a couple of lines of code: TableModel myData = new MyTableModel(); JTable table = new JTable(myData);


1 Answers

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());     // ... 
like image 63
Pops Avatar answered Nov 02 '22 15:11

Pops