Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable cell listener?

I am using a JTable. I need to get a notification whenever a cell selection change. I tried to use the ListSelectionListener but I only get notification when the row selection change. If I select a new column on the same row, I don't get notify. I need to know when the cell is selected, not when the cell is changed. Is there a listener that I can use to do this ?

like image 702
tadpole Avatar asked Sep 11 '12 19:09

tadpole


People also ask

How to get selected cell value in JTable?

getSelectedColumn(); ObjectType o = (ObjectType)target. getValueAt(row, column) ); Do this. Will get the value in your JTable based on the row and column selected and then casts the returned value to your object type in the table and returns the value at the row, column.

What is JTable used for?

The JTable class is a part of Java Swing Package and is generally used to display or edit two-dimensional data that is having both rows and columns. It is similar to a spreadsheet. This arranges data in a tabular form.

How to get selected column in JTable?

To select a column in a JTable, use the setColumnSelectionInterval() and set the interval for the column you would like to select. For example, if you want to select a single column i.e. column2, then set the interval as (2,2) in the setColumnSelectionInterval() method.

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.


1 Answers

The easiest way to do this is to call setCellSelectionEnabled(true), and pass a reference to your table to the listener. When the listener is invoked, call getSelectedRow() and getSelectedColumn() on the original table.

The alternative is to set a row selection listener on the table, a column selection listener on the ColumnModel, and then figure out their intersection.

like image 98
parsifal Avatar answered Oct 01 '22 01:10

parsifal