aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes commitEdit() is not committing the value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "commitEdit() is not committing the value" Watch "commitEdit() is not committing the value" New topic
Author

commitEdit() is not committing the value

Dalia Sultana
Ranch Hand

Joined: Jul 16, 2006
Posts: 42
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).
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
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.


bitguru blog
Dalia Sultana
Ranch Hand

Joined: Jul 16, 2006
Posts: 42
I have that property set on the table.
Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 863
    
    1
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: commitEdit() is not committing the value