• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

commitEdit() is not committing the value

 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a JFormattedTextField as an editor in a JTable cell. If the user clicks on a different cell the value that the user just entered disappears, but on an enter it persists. To resolve the problem, I added a focus listener for the text field. So on focus lost I am calling commitEdit(). The value is still not persisting. I tried to go through the code in commitEdit, which showed that

format.stringToValue(getText()) --returns the old value.

It seems like the formatter some how did not return the new value.
This is how I am initializing the text field.

NumberFormat fmt2 = NumberFormat.getNumberInstance();
fmt2.setMinimumFractionDigits(9);
JFormattedTextField yearlyRateField = new JFormattedTextField(fmt2);
yearlyRateField .addFocusListener(new FocusListener(){

public void focusGained(FocusEvent e) {}
public void focusLost(FocusEvent e) {
yearlyRateField.commitEdit();
System.out.println(yearlyRateField.getValue());//this prints the old value
//yearlyRateField.setValue(23.1) //sets the value alright
}

});

Btw, I have a table cell listener, which updates the table model with the new value. After calling commitEdit, if I explicitly set a value to the text box, that value persists (as shown in the comment).
 
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Have you tried
yourTable.putClientProperty("terminateEditOnFocusLost", Boolean.TRUE);

It seems to me that you should be able to get the behaviour you want without having to mess with focus listeners.
 
Dalia Sultana
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have that property set on the table.
 
Brian Cole
Author
Posts: 986
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Your code works for me as is (except that yearlyRateField must be final and you must catch ParseException), and it also works if I comment out the entire FocusListener.

So presumably it's not this part of the code that's causing you trouble. If you're really using this as a table editor, you should probably show us all your TableCellEditor code, and perhaps also the code that attaches it to your JTable.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic