JavaFX | Slider Class

26 Jan, 2022
A Slider is a Control in JavaFX which is used to display a continuous or discrete range of valid numeric choices and allows the user to interact with the control. A slider is rendered as a vertical or horizontal bar with a knob that the user can slide to indicate the desired value. A slider can also have tick marks and labels to indicate the intervals along the bar. The three fundamental variables of the slider are min, max, and value. The value should always be a number within the range defined by min and max. min should always be less than to max. min defaults to 0, whereas max defaults to 100.Constructors of the class:  
Slider(): Creates a default Slider instance.
Slider(double min, double max, double value): Constructs a Slider control with the specified slider min, max and current value values.
Commonly Used Methods:
Below programs illustrate the use of Slider class: 
Simple Java program to implement the Slider Class: In this program we will create a group and Scene. Add Scene to the frame. Then, create a Slider and add it to the frame. Now launch the application.
Java
// Java program to implement the Slider Classimport javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.Slider;import javafx.stage.Stage; public class SliderExample extends Application {     public void start(Stage stage)    {         // creating group        Group root = new Group();        Scene scene = new Scene(root, 600, 400);         // set Scene to the stage        stage.setScene(scene);         // set title for the frame        stage.setTitle("Slider Sample");         // create slider        Slider slider = new Slider();         // add slider to the frame        root.getChildren().add(slider);         stage.show();    }     // Main Method    public static void main(String[] args)    {         // launch the application        launch(args);    }}
Output:
Java program to implement Slider class by using TickMarks and TickLabels: In this program we will create a Group and scene. Add the scene to the frame. Create a slider with specified min, max and value. Enable the Marks and Labels. Set MajorTickUnit with the specified value. Add the Slider to the frame and display it.
Java
// Java program to implement Slider class// by using TickMarks and TickLabelsimport javafx.application.Application;import javafx.scene.Group;import javafx.scene.Scene;import javafx.scene.control.Slider;import javafx.stage.Stage; public class SliderExample extends Application {     public void start(Stage stage)    {        Group root = new Group();         // create a Scene        Scene scene = new Scene(root, 600, 400);         // add Scene to the frame        stage.setScene(scene);         // set title of the frame        stage.setTitle("Slider Sample");         // Creates a slider        Slider slider = new Slider(0, 1, 0.5);         // enable the marks        slider.setShowTickMarks(true);         // enable the Labels        slider.setShowTickLabels(true);         // set Major tick unit        slider.setMajorTickUnit(0.25f);         // sets the value of the property        // blockIncrement        slider.setBlockIncrement(0.1f);         root.getChildren().add(slider);         // display        stage.show();    }     // Main Method    public static void main(String[] args)    {         // Launch the application        launch(args);    }}
Output :
Java program to implement Slider Class using ChangeListener: In this program, we will create a Label and set the color for the text. Create a slider and set its min, max and value. Enable TickLabels and TickMarks. Set the value of the property blockIncrement. setBlockIncrement() method defines the distance that the thumb moves when a user clicks on the track. Add ChangeListener, on moving the slider the value of the brightness changes which will show in the label. Create a VBox and add to the frame. Create Scene and to the frame. Finally, launch the application.
Java
// Java program to implement Slider Class// using ChangeListenerimport javafx.application.Application;import javafx.beans.value.ChangeListener;import javafx.beans.value.ObservableValue;import javafx.geometry.Insets;import javafx.scene.Scene;import javafx.scene.control.Label;import javafx.scene.control.Slider;import javafx.scene.layout.VBox;import javafx.scene.paint.Color;import javafx.stage.Stage; public class SliderExample extends Application {     public void start(Stage stage)    {         // create label        Label label = new Label("Select the Brightness");        Label l = new Label(" ");         // set the color of the text        l.setTextFill(Color.BLACK);         // create slider        Slider slider = new Slider();         // set the value of property min,        // max and value        slider.setMin(0);        slider.setMax(100);        slider.setValue(80);         // enable TickLabels and Tick Marks        slider.setShowTickLabels(true);        slider.setShowTickMarks(true);         slider.setBlockIncrement(10);         // Adding Listener to value property.        slider.valueProperty().addListener(             new ChangeListener<Number>() {             public void changed(ObservableValue <? extends Number >                      observable, Number oldValue, Number newValue)            {                 l.setText("value: " + newValue);            }        });         // create a VBox        VBox root = new VBox();         root.setPadding(new Insets(20));        root.setSpacing(10);        root.getChildren().addAll(label, slider, l);         stage.setTitle("Slider Sample");         // create Scene and add to the frame        Scene scene = new Scene(root, 350, 200);        stage.setScene(scene);        stage.show();    }     // Main Method    public static void main(String[] args)    {         // Launch Application        Application.launch(args);    }}
Output:
Note: The above programs might not run in an online IDE. Please use an offline compiler.Reference: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/control/Slider.html
sweetyty
JavaFX
Java
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Object Oriented Programming (OOPs) Concept in Java
How to iterate any Map in Java
HashMap in Java with Examples
ArrayList in Java
Stream In Java
Collections in Java
Singleton Class in Java
Multidimensional Arrays in Java
Stack Class in Java
Set in Java