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
?
So you can call table. getSelectionModel(). isSelectionEmpty() to find out if any row is selected.
ListSelectionModel newmodel = mytable.
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.
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);
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()); // ...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With