Updated July 7, 2023
Introduction to JavaFX Slider
In JavaFX, a control known as Slider shows a continuous range of values between a specified maximum and minimum. Slider is indicated by a horizontal or vertical bar with a knob in which a user can use that to adjust the value. Tick marks and labels to mention values can also be specified in a slider. In addition to that, the values must be within the range of minimum and maximum mentioned in it. Moreover, the default value of min is 0 and the max is 100. In JavaFX, Slider can be instantiated from the package JavaFX.scene.control and Slider class.
JavaFX Slider Constructors
Below are the two constructors in JavaFX Slider:
1. Sider(): A default Slider instance gets created here.
//Creates a slider
Slider slider = new Slider();
2. Slider(double min, double max, double value): A Slider control gets constructed with the mentioned slider min value, max value, and current value.
// Create a slider
Slider slider = new Slider(0, 10, 0.5);
Methods of JavaFX Slider
Below are the methods:
- adjustValue(double nValue): In order to match new value, the value will be adjusted here.
- getMax(): Max Property’s value will be returned.
- getMin(): Min Property’s value will be returned.
- setMax(double v): Max Property’s value will be set.
- setMin (double v): Min Property’s value will be set.
- getBlockIncrement (): BlockIncrement Property’s value will be returned.
- setBlockIncrement (double v): BlockIncrement Property’s value will be set.
- decrement (): Value is decremented by the blockIncrement bounded by max.
- increment (): Value is incremented by the blockIncrement bounded by max.
- getMajorTickUnit (): Major Tick Unit Property’s value will be returned.
- getMinorTickUnit (): Minor Tick Unit Property’s value will be returned.
- setMajorTickUnit (double v): Major Tick Unit Property’s value will be set.
- setMinorTickUnit (double v): Minor Tick Unit Property’s value will be set.
- getValue(): property Value’s value will be returned.
- setValue(double v): Property Value’s value will be set.
- getValue(): Property Value’s value will be returned.
- setShowTickLabels(boolean v): ShowTickLabels Property’s value will be set.
- setShowTickMarks(boolean v): setShowTickMarks property’s value will be set.
- setValueChanging(boolean v): ValueChanging Property’s value will be set.
- isShowTickLabels(): ShowTickLabels Property’s value will be returned.
- isShowTickMarks(): ShowTickMarks Property’s value will be returned.
How to Create a JavaFX Slider?
Following are the steps to create a slider:
- Set the title for the stage created.
- Create a slider using a parameterized or non-parameterized constructor.
- Set the min value, max value and value. If nothing is mentioned, the default value will be taken.
- Create a horizontal box.
- Add the slider created to the Scene Graph using the methods setScene(), show(), etc.
Program
Now, let us see a few programs that implement the JavaFX Slider in different ways:
Example #1
Program to demonstrate the JavaFX Slider.
Code:
//Java program to demonstrate JavaFX Slider
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
//sample class
public class JavaFXSliderExample extends Application {
//main method
public static void main(String[] args) {
launch(args);
}
//application starts at this point
@Override
public void start(Stage s) {
//set the title
s.setTitle("JavaFX Slider Example");
//create the slider
Slider sl1 = new Slider(0, 100, 0);
//create verticalbox
VBox vb = new VBox(sl1);
//create scene
Scene sc = new Scene(vb, 960, 600);
//set the scene
s.setScene(sc);
//display the result
s.show();
}
}
Output:
Explanation to the above code:
- Implementing a slider is simple. First, set the title and create a slider.
- Add it to the scene graph.
- Display the results.
Example #2
Program to demonstrate JavaFX Slider with tick marks and tick labels.
Code:
//Java program to demonstrate JavaFX Slider with tick marks and labels
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
//sample class
public class JavaFXSliderExample extends Application {
//main method
public static void main(String[] args) {
launch(args);
}
//application starts at this point
@Override
public void start(Stage s) {
//set the title
s.setTitle("JavaFX Slider Example");
//create the slider
Slider sl1 = new Slider(0, 100, 0);
sl1.setShowTickMarks(true);
sl1.setShowTickLabels(true);
//create verticalbox
VBox vb = new VBox(sl1);
//create scene
Scene sc = new Scene(vb, 300, 200);
//set the scene
s.setScene(sc);
//display the result
s.show();
} }
Output:
Explanation to the above code:
- The only difference in this program from the first program is the displaying of tick marks and tick labels.
- Create the slider
- Set Tick mark and tick label functions as true.
- Set the title and create a slider.
- Add it to the scene graph.
- Display the results.
Example #3
Code:
//Java program to demonstrate JavaFX Slider with tick marks and labels
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.geometry.Insets;
import javafx.scene.control.Label;
import javafx.scene.paint.Color;
//sample class
public class JavaFXSliderExample extends Application {
//main method
public static void main(String[] args) {
launch(args);
}
//application starts at this point
@Override
public void start(Stage s) {
//set the title
s.setTitle("JavaFX Slider Example");
// create label
Label lbl= new Label("Select the Volume ");
Label lb = new Label(" ");
// Color of the text is set
lbl.setTextFill(Color.RED);
// create slider
Slider sl1 = new Slider();
// set min value, max value and value
sl1.setMin(0);
sl1.setMax(70);
sl1.setValue(50);
// set TickLabels and Tick Marks
sl1.setShowTickLabels(true);
sl1.setShowTickMarks(true);
sl1.setBlockIncrement(10);
//create vertical box
VBox vb = new VBox();
vb.setPadding(new Insets(20));
vb.setSpacing(10);
vb.getChildren().addAll(lbl, sl1, lb);
//create scene
Scene sc = new Scene(vb, 300, 200);
//set the scene
s.setScene(sc);
//display the result
s.show();
}
}
Output:
Explanation to the above code:
- In this program, several methods are present to perform operations such as displaying color of text, setting the max and min value, displaying tick marks and tick labels.
- First, Create the slider.
Set the Color of the Text
- Set Tick mark, tick label functions as true.
- Set the title and create a slider.
- Add it to the scene graph.
- Display the results.
Conclusion
JavaFX Slider displays a continuous range of values between a specified maximum and minimum using a horizontal or vertical bar with a knob that can be adjusted to set the value. Tick marks, labels, and several other functions to mention values can also be specified in a JavaFX slider depends on the requirement.
Recommended Articles
This is a guide to JavaFX Slider. Here we discuss the methods, two constructors and few programs that implement the JavaFX Slider with proper codes and outputs. You can also go through our other related articles to learn more –