Solved

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

Posted on 2011-03-07
4
511 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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

757 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now