Win a copy of Java Concurrency Live Lessons this week in the Threads forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Saving contents of a JTable  RSS feed

 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I have a swing application which includes a JTable and a button on the canvas. The JTable is editable and the function of the button is to save the contents of the table once it has been edited.
Now, when the JTable is being edited, assuming the cursor sign (I think that's what it's called) is blinking in once of the cells, and you click on the save button, an exception comes up. Is there a way of setting up my JTables so that when the mouse is not focused on them, or something, the editable state is set to false?




That is how I get the data in the table. But when a cell is being edited and I click on save, an error occurs. Any help would be appreciated.
Many Thanks,

Igwe.
 
Rob Spoor
Sheriff
Posts: 20904
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you post the Exception stack trace?
 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Rob, I just figured out that all you need to do is tab out from the cell. Thanks for the help.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's one solution. I'd like to point out that this is a bad idea though. You've just made your program very dependent on the user behaving in exactly the way you expect or it fails. You'll find that users have a way of finding the most convoluted and illogical means of using a program, so that's a bad idea.

In your case, this may not be an issue as you amy be the only user, or this may be an assignment where you can get by with it not working in certain conditions. However, if you want it to be less fragile you should either stop the editing when the user clicks the button, or detect the editing and force them to stop editing themselves before they click it.

It's really simple. In your logic that detects button clicks add something like this:



Or cancel/stop the editing yourself like so:



That would save whatever they'd put in so far. If you used cancelCellEditing() it'd revert back to what it was before they started editing it.

Or, rather than displaying a message when they click while editing, just disable the button while they're editing. I didn't include an example of this because I don't know off the top of my head how to detect when they start and stop editing. I think it uses a ChangeListener for that.
[ October 27, 2005: Message edited by: Ken Blair ]
 
igwe kalu kalu ogba
Ranch Hand
Posts: 133
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much. That is really the right solution, you're right, not everyone will be using this kind of application. It's really for the some pretty experienced programmers in my unit. But still, it helps not to explain this to everyone. Thanks a lot.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!