问题
I have a JTable which has certain cells editable. If the user enters in a value into an editable cell and then clicks the OK button on my form, the code:
// Get the value in the editable cell
String value = model.getValueAt(row, column);
does not pick up the entered value.
The only way the entered value is picked up by the above code is once the user has pressed enter after typing in the value or clicking in another cell for the value to be "pushed in".
Does someone know what code I could write that would "push" the value in when they click the OK button?
回答1:
The JTable doesn't know when you are finished typing until you use "Enter" or tab to a new cell.
The value you are typing is not saved in the TableModel until you do one of the above.
A couple of solutions:
When you create the table you can use:
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
Or, in the ActionListener you can add:
if (table.isEditing())
table.getCellEditor().stopCellEditing();
See Table Stop Editing for more information on above suggestions.
回答2:
Here is a simple code whice will help you.
if(jTable.isEditing){
jTable.stopCellEditing();
}
来源:https://stackoverflow.com/questions/5037816/enter-value-in-jtable-cell-and-click-ok-does-not-register-value