8

I'm using a Slider in my javaFX project and I have a Label that updates when I move the slider.

I want the Label to update whilst I'm dragging the Slider and not only when the drag is dropped.

This is my code:

 betSlider.valueChangingProperty().addListener(new ChangeListener<Boolean>() {
            @Override
            public void changed(ObservableValue<? extends Boolean> source, Boolean oldValue, Boolean newValue) {
                betLabel.textProperty().setValue(String.valueOf((int)betSlider.getValue()));
  } });
Patrick
  • 33,984
  • 10
  • 106
  • 126
David Tzoor
  • 987
  • 4
  • 16
  • 33
  • Alternatively, consider [_Connecting a Slider and Spinner…_](https://stackoverflow.com/q/55427306/230513). – trashgod Jan 12 '22 at 12:43

6 Answers6

12

You just need to change the valueChangingProperty() to valueProperty() and TADA, it works as you want !

A small sample is attached here :

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Demo extends Application {

    @Override
    public void start(Stage primaryStage) {
        // Add a scene
        VBox root = new VBox();
        Scene scene = new Scene(root, 500, 200);

        final Label betLabel = new Label("sdsd");

        final Slider betSlider = new Slider();
        betSlider.valueProperty().addListener(new ChangeListener<Number>() {

                @Override
                public void changed(
                   ObservableValue<? extends Number> observableValue, 
                   Number oldValue, 
                   Number newValue) { 
                      betLabel.textProperty().setValue(
                           String.valueOf(newValue.intValue());
                  }
            }
        });

        root.getChildren().addAll(betSlider, betLabel);
        betLabel.textProperty().setValue("abc");

        // show the stage
        primaryStage.setTitle("Demo");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}
JoschJava
  • 1,152
  • 12
  • 20
ItachiUchiha
  • 36,135
  • 10
  • 122
  • 176
11

Bind the label's textProperty to the slider's valueProperty.

A format conversion is required in the binding to make it work.

Either Itachi's valueProperty() ChangeListener or a binding will work.

11

import javafx.application.Application;
import javafx.beans.binding.Bindings;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Slide extends Application {
    @Override public void start(Stage stage) {   
        Label  label  = new Label();
        Slider slider = new Slider(1, 11, 5);

        label.textProperty().bind(
            Bindings.format(
                "%.2f",
                slider.valueProperty()
            )
        );

        VBox layout = new VBox(10, label, slider);
        layout.setStyle("-fx-padding: 10px; -fx-alignment: baseline-right");

        stage.setScene(new Scene(layout));
        stage.setTitle("Goes to");
        stage.show();
    }

    public static void main(String[] args) { launch(args); }
}
jewelsea
  • 150,031
  • 14
  • 366
  • 406
10

And if you want to do completely in in FXML, you can do this:

<TextField prefWidth="50" text="${speedSlider.value}"/>
<Slider fx:id="speedSlider" orientation="HORIZONTAL" prefWidth="300"
        min="60" max="100000" blockIncrement="100"/>
bluephoton
  • 379
  • 3
  • 6
0

Adding an alternative that seems simpler and easier to me:

Given a slider named slMySlider and a label named lblMySlider

  1. Add a MouseEvent.MOUSE_DRAGGED event handler to the slider and have it call a helper method:
slMySlider.addEventHandler(MouseEvent.MOUSE_DRAGGED, this::changeLabelHandler);
  1. Have the helper method change the label's text:
private void changeLabelHandler(MouseEvent e) {
        lblMySlider.setText("Value: " + String.format("%1.2f", slMySlider.getValue()));
    }
Community
  • 1
  • 1
jpdh
  • 41
  • 4
0
slider.valueProperty().addListener((observable, oldValue, newValue) ->
label.setText("sliderNameLabel: " + newValue));
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
  • 2
    Welcome, and thank you for contributing an answer. Would you kindly edit your answer to to include an explanation of your code? That will help future readers better understand what is going on, and especially those members of the community who are new to the language and struggling to understand the concepts. That's especially true here, since your answer is competing for attention with five well-established answers. – Jeremy Caney Feb 03 '21 at 19:47
-2

If you have a slider in JavaFX 8, you could do this:

slider().addListener(e -> {
    // Your code here could be anything.
});
Carsten Løvbo Andersen
  • 26,637
  • 10
  • 47
  • 77
bdshahab
  • 189
  • 1
  • 12