Converting Values Between Source and Target Properties (Java Desktop Applications)
See Also
When you bind the values of two properties of two objects,
you sometimes need to convert the values between different types.
The beans binding library contains converters for some common
conversions. For other conversions, you need to provide a
custom converter.
Below is a list of conversions for which you do not need to provide a
converter:
- BigDecimal to String, String to BigDecimal
- BigInteger to String, String to BigInteger
- Boolean to String, String to Boolean
- Byte to String, String to Byte
- Char to String, String to Char
- Double to String, String to Double
- Float to String, String to Float
- Int to String, String to Int
- Long to String, String to BigDecimal
- Short to String, String to Short
- Int to Boolean, Boolean to Int
To write a custom converter, create a class that extends
org.jdesktop.beansbinding.Converter. Your class needs to
override the convertForward(S value) and convertReverse(T value)
methods. convertForward(S value) converts a value from the source type
to the target type. convertReverse(T value) converts a value from the
target type to the source type.
To use a custom converter in a binding:
- Right-click the converter class in the Projects window and choose Compile File.
- Drag the converter from the Projects window to the Design view of your form.
The converter is added to your form as a bean.
- Right-click the target of your binding and choose Bind > TargetProperty.
- In the Bind Dialog box, select the Advanced tab.
- From the Converter drop-down list, choose the converter you have added to the form.
- Click OK.
- You can also add the conversion code directly by clicking
the ellipsis (...) button, and selecting Custom Code from the Select Converter
Property Using drop-down list.
-
See Also
- Validating Target Value Changes in Bindings
- About Beans Binding in Java Desktop Applications
- Binding Data to a Swing Component
Legal Notices