how to force cell editor in jtable to accept edits - java

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.

Related

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.

JTable erratic behavior

I have some old Java application that uses JTable to show some data and allow input as well.
JTable has 2 columns. Next to JTable, there is button that adds new row into JTable.
My problem is behavior of Tab and Enter keyboard keys and mouse clicks when you navigate JTable.
I will try to explain:
Let say there are 5 rows in JTable:
if I click with mouse on one row, sometime whole row gets highlighted, and sometime cell gets into edit mode (in addition to whole row being higlighted)
Sometime, selected row gets highlighted, but cell above or below highlighted row gets into edit mode
If I use Tab to skip from one field to another, editable field is always above highlighted row.
There are other issues as well.
Any ideas what might be wrong with it?
That sounds a bit odd. Are you fully invalidating your table and notifying listeners etc that the number of rows have changed?
In your table model, loop over all of your TableModelListeners and fire a TableModelEvent 'insert' event:
TableModelEvent event = new TableModelEvent(
modelInstance,
positionOfNewRow,
positionOfNewRow,
TableModelEvent.ALL_COLUMNS,
TableModelEvent.INSERT);
for (TableModelListener l: listeners) {
l.tableChanged(event);
}
If you can't work out the problem, it is possible to write your own behaviour for the table.
These are some useful methods:
editCellAt(int row, int column);
rowAtPoint(Point point);
columnAtPoint(Point point);
You can add your own listeners to the table to intercept events and edit cells in any way you like.
Turn off the Table cell editing. And see how it behaves. Typically this can be handled by changing the TableModel.isCellEditable() method to simply return false.
Turn it back on. Is your instance of JTable subclassed? Is it overriding editCell()? If so that's the method that will trigger an edit based on the event occurring or not. That method is turning on editing inappropriately. The implementation of that method is bad if it's overridden.
If your table isn't subclassed look for calls to editCell(). Those are probably calling it inappropriately so you can look for those calls and start setting breakpoints or log statements.

How to handle JTable data changed event

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!

Force JTable to "commit" data to model while it is still in editing mode

I have a JTable as follow.
So, while the JTable is still in editing mode (There is a keyboard cursor blinking at Dividend column), clicking OK directly will not commit the data into table model. Clicking OK merely close the dialog box.
I need to press ENTER explicitly, in order to commit the data into table model.
While JTable is still in editing mode, before closing dialog box, is there any way I can tell the JTable by saying, "Hey, is time for you to commit the changes into your model"
The source code for this dialog box is as follow Dialog Box Source Code. Do look at jButton1ActionPerformed for the executed code when OK is pressed.
I'm not sure if it will work (it would have been nice to have a SCCE), but try this:
TableCellEditor editor = table.getCellEditor();
if (editor != null) {
editor.stopCellEditing();
}
Table Stop Editing gives a couple of approaches.
EDIT
Example from article:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Example from article:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
To make the whole stable stop editing completely in any state (editing or not), you can call editing stopped:
table.editingStopped(new ChangeEvent(table));
That way you don't have to check for editors/state/etc.

Categories

Resources