Marcel
Marcel

Reputation: 1768

Convert DoubleBinding value to IntegerBinding

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

Upvotes: 0

Views: 410

Answers (2)

Rahel Lüthy
Rahel Lüthy

Reputation: 7007

You can use Bindings.format instead:

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

Upvotes: 2

Uluk Biy
Uluk Biy

Reputation: 49205

You can cast double to int:

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

Upvotes: 1

Related Questions