Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Value into slider thumb in JavaFX

Tags:

javafx

It is possible to display the value of a slider within his thumb in JavaFX like this? Slider with info within his thumb

like image 393
Zuri Avatar asked Jan 06 '23 18:01

Zuri


1 Answers

You can do this with a lookup: you have to assume the thumb is a Pane (or a subclass thereof):

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SliderWithLabeledThumb extends Application {

    @Override
    public void start(Stage primaryStage) {
        Slider slider = new Slider();
        StackPane root = new StackPane(slider);
        root.setPadding(new Insets(20));

        Scene scene = new Scene(root);

        slider.applyCss();
        slider.layout();
        Pane thumb = (Pane) slider.lookup(".thumb");
        Label label = new Label();
        label.textProperty().bind(slider.valueProperty().asString("%.1f"));
        thumb.getChildren().add(label);


        primaryStage.setScene(scene);
        primaryStage.show();

    }

    public static void main(String[] args) {
        launch(args);
    }
}

enter image description here

like image 97
James_D Avatar answered Jan 08 '23 08:01

James_D