Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

I have a JTable as follow.

alt text

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.

like image 383
Cheok Yan Cheng Avatar asked Oct 07 '10 16:10

Cheok Yan Cheng


3 Answers

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();
like image 196
camickr Avatar answered Oct 21 '22 09:10

camickr


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();
}
like image 30
Guillaume Avatar answered Oct 21 '22 08:10

Guillaume


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.

like image 33
John Gardner Avatar answered Oct 21 '22 08:10

John Gardner