Leprechaun
Leprechaun

Reputation: 971

How to bind JLabel to JSlider using division?

I need to make a slider that allows user to input a number between 0 and 1 in steps of 0.1. Since JSlider only allows integer values, I gave it a range from 0 to 10 and binded a JLabel to the slider's value. But I want the label to display the decimal number (value/10.0). How can I do that? I am using Netbeans 7.4 GUI editor. When I try to edit the binding expression to ${value}/10 it just appends it as a string. The Component chooser window in converter only says "Custom editing of this property is not supported." Thanks

Upvotes: 1

Views: 264

Answers (1)

hoefling
hoefling

Reputation: 66251

If you want to use BeansBinding: The EL language as realized in BeansBinding library does not support most of the features including primitive calculations. You have to implement a custom org.jdesktop.beansbinding.Converter and then add it using "custom code" option: In the converter adding panel, select "Custom code" in converter property combobox, then instantiate your converter in "Property code" text field.

Also, consider writing your own ChangeListener that you attach to JSpinner which will update the label's text each time the spinner value will change. This is way more easy as I see it. :-)

Upvotes: 2

Related Questions