Solved

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

Posted on 2011-03-07
4
519 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 95 total points
ID: 35061333
0
 
LVL 26

Assisted Solution

by:ksivananth
ksivananth earned 30 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java Server Faces parameter pass? 6 48
Weekend adv creator 3 36
Oracle SQL syntax check  without executing 6 50
Glassfish admin console not working 1 10
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.

774 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