• Post Reply Bookmark Topic Watch Topic
  • New Topic

JTable doesn't recognise update until field is exited  RSS feed

 
Keith L
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The fact that JTable only calls the SetValueAt() method when a user leaves an updated cell is causing me a problem. If the user closes the application before tabbing out of the cell (or clicks onto another tab of a tabbed pane) , my code in SetValueAt() is never executed.

I'd appreciate any suggestions on the best way to deal with this. Do I need to set up a listener to detect whenever any character is modified rather than waiting for the user to leave the cell?

Thanks,
Keith
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try adding a focusListener to the JTable and call setValueAt yourself when focus is lost.
 
Keith L
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply. I was moving in that direction myself. The problem with calling SetValueAt()myself is that I need to give it the row and column of the cell currently being edited. I'm not sure where to get that.

My thought was to set up a focus listener and then call:

table.getCellEditor().stopCellEditing();

This is almost working but I get a null pointer error if the focus is lost when a cell has been selected but not edited.
 
Stuart Gray
Ranch Hand
Posts: 410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Presumably it is the CellEditor that is null if the cell isn't being edited (though the API docs don't say this). In that case I would just use this, which looks a bit nicer than catching an exception IMHO:
 
Keith L
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This works:

if ( table.isEditing())
table.getCellEditor().stopCellEditing();

but I came across this which looks as though it might be simpler:

envtable.editingStopped(new ChangeEvent());
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!