aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes TableCellEditor - edit value of last cell before table focus lost Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "TableCellEditor - edit value of last cell before table focus lost" Watch "TableCellEditor - edit value of last cell before table focus lost" New topic
Author

TableCellEditor - edit value of last cell before table focus lost

Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there

I have looked on these forums and on the net for the answer to this question. The problem seems quite common, but I can't find the solution.

On my JTable I have a TableCellEditor. It is working for every cell except the final cell that gets edited before the table loses focus. For example, if I edit a cell and then click a button, that specific edit does not get saved.

Where does this get processed so that I can catch it?

Many kind regards,
Rachel
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Your class should implement TableModelListener and include a tableChanged method.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi.

Thanks for the suggestion, but that doesn't fix the problem.

Cheers,
Rachel
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
Your tableChanged method will be called every time a cell in your table is modified.

Including the last one.

If, inside of this method, you can see the new value for that cell, then the problem must be elsewhere.

Can you provide any code?
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
If, inside of this method, you can see the new value for that cell, then the problem must be elsewhere


I can't see the value inside that method.


OK. Don't let the class names fool you, it's for a translation software for our website.

code for the table model


code for the cell editor


[ November 01, 2004: Message edited by: Rachel Swailes ]
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
When you issue this statement:



I'm assuming that you are only interested in this one element, right?

ok, given that, what is the output you see from this statement? Is it the
"old value" (the value held in the table before you edited it) or a null value or perhaps something else?
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
I just wanted to print out this row because this is the info I need to get a handle to in order to save it.
I changed the code for the tableChanged to look like this...



So here is the output for the following scenarios:

1 - Write in a cell and tab to another cell

Row Information: Hello

2 - Write in a cell and click somewhere else on the program (in this case, the list that generates a new table

Null Pointer exception

3 - Write in a cell and then click the close button (calls System.exit(0));

Row Information: Hello


4 - Write in a cell and then click the x button to close.

Null Pointer exception
[ November 02, 2004: Message edited by: Rachel Swailes ]
jefff willis
Ranch Hand

Joined: Sep 29, 2004
Posts: 113
When you say "write in a cell" I'm assuming that you mean column 1 and not column 0 (since that is what you are printing).

I can't say I'm entirely clear why you are using this call in your code:


If you have built this table from the contents of your "rows" object, then I might suggest that you use fireTableDataChanged() in your setData() method.

Also, check to make sure that this statement:


isn't returning a null under some condition.
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Hi there.

I just wanted to say thanks for the help you are giving to help me get this right. I very much appreciate it!

I changed the code to use the fireTableDataChanged and not I don't have to catch that null pointer exception in the tableChanged anymore. So that's cool.

Yes, only column[1] is editable.

The getDestinationKeyPair doesn't return a null at any time.

So after those additions these are the results in the four scenarios...
1 - Edit cell and click to another cell - Works
2 - Edit cell and click close (calling system.exit) - Works
3 - Edit cell and click x button - Doesn't work but I've decided that that's right so we'll forget this one.

So the last scenario to get working is the following.
On my app, there is a list. When clicking on the list, the table is changed to show the information that is detailed by the item that is selected in the list.
What is happening now is that when the user edits a cell and then clicks on the item in the list, I then get an ArrayOutOfBounds Exception in the setValue of the table model. I have a feeling that this is because the data in the table is changing at that moment so I'm going to try and get the click on the list to first save the changes and then show the new table data.

Do you think my suspicions sound right?

Cheers, and many many thanks again!
Rachel
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Yes, it did work. I told the TableCellEditor to stopEditing whenever I click on the list. It's not the best way to do it I'm sure, but it works!

Thanks for putting up with me being slow about this.

Cheers,
Rachel
Sebastiaan Kortleven
Ranch Hand

Joined: Mar 12, 2004
Posts: 81
Perhaps it's better to adde a focuslistener to the celleditor and in the focusLost method add a call to the stopCellEditing of the editor..

That way you can select other components then just your list..
Rachel Swailes
Ranch Hand

Joined: May 18, 2004
Posts: 434
Thanks, I'll keep that in mind!
 
wood burning stoves
 
subject: TableCellEditor - edit value of last cell before table focus lost