JavaFX | Slider Class
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:
Method | Description |
---|---|
adjustValue(double newValue) | Adjusts value to match newValue. |
decrement() | Decrements the value by blockIncrement, bounded by max. |
getBlockIncrement() | Gets the value of the property blockIncrement. |
getMax() | Gets the value of the property max. |
getMin() | Gets the value of the property min. |
getMajorTickUnit() | Gets the value of the property majorTickUnit. |
getMinorTickCount() | Gets the value of the property minorTickCount. |
getValue() | Gets the value of the property value. |
increment() | Increments the value by blockIncrement, bounded by max. |
setBlockIncrement(double value) | Sets the value of the property blockIncrement. |
setMajorTickUnit(double value) | Sets the value of the property majorTickUnit. |
setMax(double value) | Sets the value of the property max. |
setMin(double value) | Sets the value of the property min. |
setMinorTickCount(int value) | Sets the value of the property minorTickCount. |
setValue(double value) | Sets the value of the property value. |
setValueChanging(boolean value) | Sets the value of the property valueChanging. |
setShowTickMarks(boolean value) | Sets the value of the property showTickMarks. |
setShowTickLabels(boolean value) | Sets the value of the property showTickLabels. |
isShowTickLabels() | Gets the value of the property showTickLabels. |
isShowTickMarks() | Gets the value of the property showTickMarks. |
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 Class import 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 TickLabels import 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 ChangeListener import 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
Please Login to comment...