?
Solved

Java JTable clicking out of a field that is being edited does not update field values

Posted on 2011-03-07
4
Medium Priority
?
553 Views
Last Modified: 2012-06-21
I display a JTable with editable text columns, when the user double-clicks on a column to edit a field, then clicks on another column, the field that was edited gets updated.  But if the user double-clicks on a column to edit the field, makes changes, then presses a JButton somewhere on the same form, the table value is not updated automatically.  (The JButton code uses the values in the table, and is not getting the latest value from that last field that was being edited.)

Is there a way to make JTable automatically update a field that's being edited when the focus changes to something else?  Or at least is there some call I can put in the JButton's code to say "Finish/update and edits that were being done on the JTable"?

0
Comment
Question by:amp834
  • 2
4 Comments
 
LVL 92

Accepted Solution

by:
objects earned 380 total points
ID: 35061333
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 120 total points
ID: 35061565
0
 

Assisted Solution

by:amp834
amp834 earned 0 total points
ID: 35063635
Thanks for the replies, I will test them out tomorrow.  The reference objects sent has a link to
http://tips4java.wordpress.com/2008/12/12/table-stop-editing/

It has two good solutions I can use!


The first is to have the table automatically stop editing and save the data. This can be done by setting a property on the table:

JTable table = new JTable(...);
table.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

Note that this solution will cause the table to stop editing when any component get focus. I believe this logic is limited to components on the current window, but must admit I haven’t tested it. This solution will also not work when you click on the window buttons (ie, minimize or close).

Maybe the above solution is too general, and you want to control which components should stop editing. In this second case, you would need to add some code to the ActionListener of your buttons, something like:

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

Author Closing Comment

by:amp834
ID: 35120686
Thanks for the help, I used object's suggestion (the link was clearer, for me anyway), and also gave credit for ksivananth for helping as well.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses
Course of the Month17 days, 8 hours left to enroll

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question