posted 17 years ago
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).