How to handle JTable data changed event - java

I have a JTable bound to a List property. I used NetBeans to add a property to my jpanel form, enabled/checked propertyChangeSupport, and bound my jTable to that property. Table is displaying the data perfectly. Please guide me how can I enable/disable a 'Save' button when data of this table is changed by double-clicking a cell and editing its contents.

The Table Cell Listener will listen for real changes in the data of a cell.
A TableModelListener fires an event even if you tab out of the cell and don't actually change the data.

Have you tried adding a TableModelListener to the JTable's model? This should fire any time the table's data is changed, and the listener can then enable your save button. The button should disable itself whenever it is pressed and the data has been successfully saved.
Edit:
Please ignore this and instead go with camickr's recommendation!

Related

JTable: defining a nested table with clickable cells

I build a JTable in which every cell is a JTable itself. I need to make the cells of the nested (inner) tables clickable so that some additional information will be displayed in a pop-up window on a mouse click.
What is the best way to do this? Should I define every cell as a button?
Thank you in advance.
Instead, create two instances of JTable, master and detail. In a ListSelectionListener added to master, update the model displayed by detail using setModel(), as shown here.

Jtable disable hover selected row

i have a little problem with jtable :
when i click on the Appliquer utton, he clera all the table except the in-write cell, so i need to unset in-write mode??
that how it look like when i have clicked Appliquer button :
You are still editing the data in the cell so it has not bee saved to the TableModel yet.
See Table Stop Editing for a couple of solutions that will make sure the data is saved when you click a button.
Try to use:
table.clearSelection();
Or for a particular cell use.
columnModel.getSelectionModel().clearSelection();

JAVA: JCheckBox lost selection in a JTable when Button pressed

I have this problem: added a jcheckbox in a jtable. In my model this jcheckbox is loaded with getValueAt and used the method setValueAt when selected by the user.
No problem until I push the confirm button, when all my row selected (true value) disappear and the loop doesn't read anything with true value.
Where is my mistake? How can I update the model before pressing my confirmation button?
Sounds like you are looking for:
table.putClientProperty("terminateOnFocusLost", true);
This tells the table to try to commit any ongoing edits when the focus is transfered to the outside, that is to a component which is not a child of the table.

Cell editor not exiting when row is deleted

I have a JTable with a several columns, one of which is has a custom renderer to display 3 buttons in a JPanel as well as a custom editor to allow them to be clickable. One of the buttons sends a delete command to our server for that row id then reloads the table data from the server by clearing the data model and loading data again. When this happens the cell with the 3 buttons continues to display (but not the rest of the row) until I click another button in another row even though the row is gone.
I've set putClientProperty("terminateEditOnFocusLost", Boolean.TRUE) but changing focus to another component does not help.
I've tried deleting the row itself before refreshing the data (even deleted all the rows) and made sure to call fireTableRowsDeleted().
I've also tried calling the cancelCellEditing() and stopCellEditing() functions of the TableCellEditor and even manually setting the editing row/column to another cell.
Any help would be greatly appreciated.
Ok, so I figured it out. I ended up calling removeEditor() on the table and that fixed it.
Thanks for the responses.
I've set putClientProperty("terminateEditOnFocusLost", Boolean.TRUE) but changing focus to another component does not help.
That method should be invoked when you create the table, not in the actionPerformed method when you click the button.
Ok, so I figured it out. I ended up calling removeEditor() on the table just before the data refresh and that fixed it.
Thanks for the responses.

how to force cell editor in jtable to accept edits

i have a jtable with multiple editable cells
when someone presses the "save button" on the menu, it doesn't save the last value that is still currently being edited. (unless they tab out of the cell first)
i can check it is being edited by calling .isEditing() which returns true.
What i would prefer to do is trigger that the cell editing is complete, and it can show any validation errors, and if none, then save. (without the user having to tab out first)
can someone please point me in the right direction
thanks
You can manually call:
JTable table;
table.getCellEditor().stopCellEditing();
This should cause the table to stop editing at that cell. By overriding the stopCellEditing() method of your TableCellEditor and returning false under certain circumstances, you can indicate that the cell's value is currently invalid and thus editing cannot be stopped at that time.
when you have a jtable with multiple editable cells
when someone presses the "save button" on the menu, it doesn't save the last value then please add the below peace of code
The first is to have the table automatically stop editing and save the data. This can be done by setting a property on the table:
JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
It will make sure that when someone presses the any button on the menu, it saves the last edited value also.
If I remember correctly, edited/added value is present in cell editor component by default it is EditBox, BUT do not present in table model. If it is possible try to check cell editor component it should help you.

Categories

Resources