JTable Editing not take effect until hit Enter

R

RC

Hello dudes,

I found out if you editing a JTable cell without hit
the Enter or Tab key. Then the new cell value won't take
effect. For example, a table cell value is "abc" and
you change it to "xyz". Then when you read that table
cell value, it still "abc". You MUST hit the Enter or
Tab key, then you will get that table cell value as "xyz".

How can I force that table cell value as "xyz" WITHOUT
hit the Enter or Tab key?

Thank Q very much in advance!
 
T

Thomas Fritsch

RC said:
I found out if you editing a JTable cell without hit
the Enter or Tab key. Then the new cell value won't take
effect. For example, a table cell value is "abc" and
you change it to "xyz".
The problem is, that in this case the cell editor's stopCellEditing()
method is not called. See also the API doc of CellEditor#stopCellEditing()
Then when you read that table
cell value, it still "abc". You MUST hit the Enter or
Tab key, then you will get that table cell value as "xyz".

How can I force that table cell value as "xyz" WITHOUT
hit the Enter or Tab key?
You have to call your cell editor's stopCellEditing() method at appropriate
times (probably when the cell looses keyboard focus):
JTable yourTable = ...;
if (yourTable.isEditing())
yourTable.getCellEditor().stopCellEditing();

Or simpler: somewhere at the beginning you tell your JTable to do those
stopCellEditing calls automatically for you:
yourTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top