Skip to main content

jGoodies - Binding JFormatterTextField to int field

1 reply [Last post]
Anonymous

Hi,

I am trying to use the bindings in jGoodies to bind a JFormatterTextField to an int field on a domain object.

The text field is populated with the value from the domain object fine. When I edit the field and commit the value (hit return / move focus) I get an InvocationTargetException.

I think this may be because the JFormattedTextField turns the entered text (a number) into a Long. When the binding uses this to set the value on the bean it fails because my method takes an int not a long.

I am using a couple of classes I wrote to avoid having to use bound properties. Those classes work fine for String values so I don't think there's a general problem with the classes I wrote. You can see the class I wrote being mentioned in the stack trace (BasicPropertyValueModel).

I think this is particular to user input in a JFormattedTextField - the binding has to decide at some point whether to construct an Integer or a Long right ?

Could someone let me know whether this is a way to make this work ?

Thanks,
Alan.

-----------------------------------------

public class SomeClass {

public int mSomeIntProperty;

public int getSomeIntProperty() {
return mSomeIntProperty;
}

public void setSomeIntProperty(int someIntProperty) {
mSomeIntProperty = someIntProperty;
}

}

-----------------------------------------

mFormattedTextField = new JFormattedTextField(NumberFormat.getIntegerInstance());

pane.add(mFormattedTextField, constraints);

Bindings.bind(mFormattedTextField, model.getValueModel("someIntProperty"));

-----------------------------------------

java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at com.jgoodies.binding.beans.BeanUtils.setValue(BeanUtils.java:449)
... 33 more
Caused by: java.lang.RuntimeException: java.lang.IllegalArgumentException: argument type mismatch
at evaluation.jgoodies.BasicPropertyValueModel.setValue(BasicPropertyValueModel.java:65)
... 38 more
Caused by: java.lang.IllegalArgumentException: argument type mismatch
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:324)
at evaluation.jgoodies.BasicPropertyValueModel.setValue(BasicPropertyValueModel.java:61)
... 38 more

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
karsten
Offline
Joined: 2003-06-11
Points: 0

Hi,

You have configured the JFormattedTextField to use an integer format,
but you have not set the formatter's value class to Integer. And so,
the JFormattedTextField's 'value' property may return Longs, which
is incompatible with your bean setter that accepts int/Integer.

I provide a factory that assists you in creating formatted text fields
for different number types, including Integer. The BasicComponentFactory
will set the proper 'valueClass' to the NumberFormatter used. See
the methods #createIntegerTextField and their JavaDocs for details.

I recommend to favor the BasicComponentFactory over the Bindings class.
The latter is primarily intended for users that want to work with
customized versions of Swing components. If you can work with the
JTextField, JFormattedTextField, etc, the factory is easier to use.

Also, you can find examples that demonstrate the BasicComponentFactory
in the JGoodies Binding tutorial sources that ship with the distribution
in folder 'src/tutorial'.

Last but not least, if you have questions about the JGoodies Binding,
I suggest to post to the Binding user mailing list here at java.net.
For details see the support instructions in the Binding's README.html
or at https://binding.dev.java.net/

Hope this helps. Kind regards,
Karsten Lentzsch