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 program to implement the Slider Classimportjavafx.application.Application;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.scene.control.Slider;importjavafx.stage.Stage;publicclassSliderExampleextendsApplication {publicvoidstart(Stage stage){// creating groupGroup root =newGroup();Scene scene =newScene(root,600,400);// set Scene to the stagestage.setScene(scene);// set title for the framestage.setTitle("Slider Sample");// create sliderSlider slider =newSlider();// add slider to the frameroot.getChildren().add(slider);stage.show();}// Main Methodpublicstaticvoidmain(String[] args){// launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput:
- 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 program to implement Slider class// by using TickMarks and TickLabelsimportjavafx.application.Application;importjavafx.scene.Group;importjavafx.scene.Scene;importjavafx.scene.control.Slider;importjavafx.stage.Stage;publicclassSliderExampleextendsApplication {publicvoidstart(Stage stage){Group root =newGroup();// create a SceneScene scene =newScene(root,600,400);// add Scene to the framestage.setScene(scene);// set title of the framestage.setTitle("Slider Sample");// Creates a sliderSlider slider =newSlider(0,1,0.5);// enable the marksslider.setShowTickMarks(true);// enable the Labelsslider.setShowTickLabels(true);// set Major tick unitslider.setMajorTickUnit(0.25f);// sets the value of the property// blockIncrementslider.setBlockIncrement(0.1f);root.getChildren().add(slider);// displaystage.show();}// Main Methodpublicstaticvoidmain(String[] args){// Launch the applicationlaunch(args);}}chevron_rightfilter_noneOutput :
- 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 program to implement Slider Class// using ChangeListenerimportjavafx.application.Application;importjavafx.beans.value.ChangeListener;importjavafx.beans.value.ObservableValue;importjavafx.geometry.Insets;importjavafx.scene.Scene;importjavafx.scene.control.Label;importjavafx.scene.control.Slider;importjavafx.scene.layout.VBox;importjavafx.scene.paint.Color;importjavafx.stage.Stage;publicclassSliderExampleextendsApplication {publicvoidstart(Stage stage){// create labelLabel label =newLabel("Select the Brightness");Label l =newLabel(" ");// set the color of the tesxtl.setTextFill(Color.BLACK);// create sliderSlider slider =newSlider();// set the value of property min,// max and valueslider.setMin(0);slider.setMax(100);slider.setValue(80);// enable TickLabels and Tick Marksslider.setShowTickLabels(true);slider.setShowTickMarks(true);slider.setBlockIncrement(10);// Adding Listener to value property.slider.valueProperty().addListener(newChangeListener<Number>() {publicvoidchanged(ObservableValue <?extendsNumber >observable, Number oldValue, Number newValue){l.setText("value: "+ newValue);}});// create a VBoxVBox root =newVBox();root.setPadding(newInsets(20));root.setSpacing(10);root.getChildren().addAll(label, slider, l);stage.setTitle("Slider Sample");// create Scene and add to the frameScene scene =newScene(root,350,200);stage.setScene(scene);stage.show();}// Main Methodpublicstaticvoidmain(String[] args){// Launch ApplicationApplication.launch(args);}}chevron_rightfilter_noneOutput:
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
Recommended Posts:
- JavaFX | Tab Class
- JavaFX | Pos Class
- JavaFX | Pane Class
- JavaFX | AnchorPane Class
- JavaFX | FlowPane Class
- JavaFX | Group Class
- JavaFX | TextFlow Class
- JavaFX | HBox Class
- JavaFX | BorderPane Class
- JavaFX | StackPane Class
- JavaFX | Font Class
- JavaFX | FontPosture Class
- JavaFX | LinearGradient Class
- JavaFX | DirectoryChooser Class
- JavaFX | FontWeight Class
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



