?
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
?
542 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

801 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