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 ?
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.
One way to receive notification on column selection changes - as already answered by #parsifal(in the comments - is to grab the TableColumnModel's internal selectionModel and register a listener:
table.getColumnModel().getSelectionModel().addListSelectionListener(selectionListener);
Another way is to register a TableColumnModelListener with the columnModel:
table.getColumnModel().addColumnModelListener(columnModelListener);
The first is "shorter" in terms of code: just one method to implement vs. several - most empty except the columnSelectionChanged.
The second is more robust against dynamic changes: with the first there is no possibility to guard against changes of the selectionModel property of the columnModel ... because it is not a property. Or in other words: in the (concededly rare) case that application code swaps out the selectionModel the listener is listening to the Void. Installing a columnModelListener is immune against such a change, as the columnModel passes on the events from its selectionModel whichever it would be.
Related
When I click to select a row in a Jtable the ListSelectionListener fire twice one for selecting and another for deselecting the row so the user get the impression that the row wasn't select as it occurs very quickly.
How can I prevent the row's deselection?
PS: The default behavior without implementing ListSelectionListener works as expected when the user click the row keeps selected.
You can check with e.getValueIsAdjusting if it is first event or second.
From javadoc:
Returns whether or not this is one in a series of multiple events,
where changes are still being made.
I have instances of JTable who listen for some remote events, and depending on the type of event I want to lock/unlock a row with model index N. By Locking a row I mean setting the editable flag for all its cells to false. This is handled by my table model.
At the moment, I am calling repaint() because I did not want my table model to fire up the updated event, which is another alternative, but I am having a complex logic which is happening when the actual data in a row are changed, and did not want to trigger that logic.
Is there any other way?
Changes should be made directly to the TableModel. The TableModel will then invoke the appropriate fireXXX() method and the table will automatically repaint the rows affected.
Since it sounds like this "editable flag" is not visible on the table then there is really no reason to repaint the table, so you can override the TableModel to not generate any event when this flag is changed.
I've created a simple JTable with check box like below:
DefaultTableModel model = new DefaultTableModel();
jTable1.setModel(model);
model.addColumn("No:", no1);
model.addColumn("Remark", remark1);
model.addColumn("Color", colors1);
model.addColumn("Done");
TableColumn col1 = jTable1.getColumnModel().getColumn(0);
col1.setPreferredWidth(1);
TableColumn col4 = jTable1.getColumnModel().getColumn(3);
col4.setCellEditor(jTable1.getDefaultEditor(Boolean.class));
col4.setCellRenderer(jTable1.getDefaultRenderer(Boolean.class));
col4.setPreferredWidth(50);
jTable1.setShowGrid(true);
jTable1.setGridColor(Color.BLACK);
jTable1.setAutoCreateRowSorter(true);
It's working fine but how to do if I want to add action listener for the check box. For an example, when my check box is checked I need to pop up a confirmation message.
For an example, when my check box is checked I need to pop up a
confirmation message.
You don't need to add an ActionListener to the renderers/editors but you need to listen to table model data changes. Take a look to Listening for Data Changes section of How to Use Tables tutorial:
Add a new TableModelListener to your TableModel
Validate if the updated cell value is a Boolean and its value is true.
Ask the user to confirm the update. If s/he doesn't then set the cell's value back to false.
See TableModelEvent API as well.
Edit
Note in this case as you're working with booleans then there's 2 possible values to do the check. However for input validation in other cases the described procedure won't work simply because the listener will be notified when the change already happened and you won't be able to set the value back just because it won't be there any longer.
Take a look to #kleopatra's answer to this question: JTable Input Verifier. As stated there a better approach is providing a custom CellEditor and do the validation in stopCellEditing() method implementation. Just as a suggestion I'd use a DefaultCellEditor that takes a JCheckBox as parameter and override the aforementioned method.
I have a JTable with the required values. After editing a cell, if I use table.getvalue(row,column), I get the previous unaltered values. But if I select another cell before clicking the save button I get the modified values. Can anyone help me to remove this problem??
PS: I have not yet added any actionlisteners for the table
The default update mechanism only changes the model when the cell editor loses the focus. Either tabbing out of the cell or clicking in a different cell will cause the vital "focus lost" event which triggers the model change
You could add an ActionListener (see http://download.oracle.com/javase/tutorial/uiswing/components/textfield.html). It will get triggered when you press RETURN. In the handler, call fireEditingStopped() to trigger the "copy to model" code (see http://download.oracle.com/javase/tutorial/uiswing/components/table.html#editor).
or add following code to your table,
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Try TableModel.
Every table object uses a table model object to manage the actual table data. A table model object must implement the TableModel interface. If the programmer does not provide a table model object, JTable automatically creates an instance of DefaultTableModel.
A table model can have a set of listeners that are notified whenever the table data changes. Listeners are instances of TableModelListener.
Have you tried this
int row=table.getSelectedRow();
int column=table.getSelectedColumn();
table.getValue(row,Column)
If Yes you need to use TableModel
Is there any way to detect a cell selection change in a JTable? I've found documentation for detecting a row change using ListSelectionListener but it doesn't seam to work when changing selection on the same row. I'm using JTable to render a simple schedule.
Maybe I should use a different component?
No, the right component for showing tabular data is JTable.
You want to add a listener to the TableModel that's underneath the table. That will fire off events whenever data changes. You get it out of JTable, unsurprisingly enough, by calling getTableModel().
Update
Oh wait, I think I misunderstood you. You're not interested in data changes but column selection changes.
JTable has a method called columnSelectionChanged; its documentation says it's called by TableColumnModelListener, which leads me to believe that what you want to do is getColumnModel() and use the addColumnModelListener() method of that to listen for column selection changes.