Using JFormattedTextField with Swing Data Binding from tornado.no

Although there are many data binding libraries available for Swing, the one I have been dealing with lately is from http://databinding.tornado.no/

After I added a JFormattedTextField as a bound field, I noticed that it wasn’t obeying the text-to-value and value-to-text conversion that I had specified in my Formatter. Instead, the data binding was trying to populate the model with the text from the field instead of the value. A look at the source code of CoreUIBridgeProvider shows that JTextFieldBridge is used for JFormattedTextFields, and that bridge only gets & sets the text of the field. Its behavior is sufficient for JTextField, but not for proper use of JFormattedTextField.

To fix the issue, I wrote a custom bridge specifically for formatted fields. It simply passes the value object straight through it.

@Override
public void setUIValue(JFormattedTextField component, Object value) throws ConversionException {
    component.setValue(value);
}

@Override
public Object getUIValue(JFormattedTextField component) throws ConversionException {
    return component.getValue();
}

After I added a new entry to my bridge provider to map my new bridge to my custom JFormattedTextField class, it immediately started working as expected.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.