Convert DoubleBinding value to IntegerBinding

404 views Asked by At

I have the following Problem. I want to concat a String with an DoubleBinding to an String property.

Slider slider = new Slider(1, 5000, 500);
Label sliderValue = new Label();
sliderValue.textProperty().bind(Bindings.concat("Sleep: ", slider.valueProperty()));

This is working fine.

But I don't want to display the Double Binding as an Double, I want to round it to an int.

As you can see in the screenshot the Number is added as an Double to String.

Is there any way to do that, without creating an Listener?

Sry if my english it not correct. :P

2

There are 2 answers

0
Rahel Lüthy On BEST ANSWER

You can use Bindings.format instead:

sliderValue.textProperty().bind(
  Bindings.format("Sleep: %.0f", slider.valueProperty())
);
1
Uluk Biy On

You can cast double to int:

sliderValue.textProperty().bind( Bindings.concat( "Sleep: ", 
   Bindings.createIntegerBinding( () -> (int) slider.valueProperty().get(), slider.valueProperty()) ) );