JTable data entry

Hi,

When I asked my question in the "New to Java" forum, I was redirected here. Here is my question:

I've just created my first JTable (inside a JScrollpane, inside a JPanel, inside a JFrame). It seems to work ok, except that when I type data into cells, the data in the last cell I edit isn't saved to the model when I "leave" the table unless I hit return after typing it in or click somewhere else in the table before "leaving." And occasionally, I forget to do that. (After all, the table *looks* right after I'm done typing.)

When I do leave the table, it's usually to press a JButton that calls a method that does a few calculations and inserts a new blank row into the model at row index 1. Ideally, I'd like that when I hit that JButton, it forces the JTable to update the model with the data typed and pending in the last cell I edited, even if I have forgotten to hit return or click someplace else first.

Is there a way to force that pending data to be saved?

Jim

[1016 byte] By [JamesRomanoa] at [2007-11-27 3:43:21]
# 1
You might try adding a FocusListener to the cell editor component and then update the table model in the focusLost event.
BinaryDigita at 2007-7-12 8:47:00 > top of Java-index,Desktop,Core GUI APIs...
# 2
You need a reference to the table, then you can say:if(table.isEditing()) {table.getCellEditor().stopCellEditing();}
HansBickela at 2007-7-12 8:47:00 > top of Java-index,Desktop,Core GUI APIs...
# 3
Thank you both for your answers.HansBickel's answer was just what I needed. I added the code, tested it, and it works perfectly. Good job!Jim
JamesRomanoa at 2007-7-12 8:47:00 > top of Java-index,Desktop,Core GUI APIs...
# 4
When you create your table you add the following, then you don't need to worry about using the above code for every component on your GUI.table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);
camickra at 2007-7-12 8:47:00 > top of Java-index,Desktop,Core GUI APIs...