Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make a label update while dragging a slider

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()));
  } });
like image 743
David Tzoor Avatar asked Apr 01 '14 08:04

David Tzoor


2 Answers

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);
    }
}
like image 67
ItachiUchiha Avatar answered Sep 18 '22 20:09

ItachiUchiha


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); }
}
like image 38
jewelsea Avatar answered Sep 19 '22 20:09

jewelsea