×

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range of values. In order to use Slider in JavaFX, javafx.scene.control.Slider class is used. The slider can be a vertical or horizontal bar depending upon user requirements. The slider has tick marks on it. It has min, max, value variable showing different marks on the slider. These values are numbers.

JavaFX Slider:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.stage.Stage;  


  
public class SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Group root = new Group();
  
        Slider slider = new Slider();
  
        Scene scene = new Scene(root, 600, 400);
        root.getChildren().add(slider);
        primaryStage.setScene(scene);  
        primaryStage.setTitle("JavaFx Slider Example");  
        primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Slider in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.Slider, javafx.scene.Group.

Then we have created one class named SliderUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold the slider, Group is created which is then passed to the Scene class object.

The Slider is created with its constructor.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, “ JavaFX Slider Example ". Also, it displays a Simple Slider.

JavaFX Slider

JavaFX Slider with Tick Marks:

Example:

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.stage.Stage;  


  
public class SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Group pane = new Group();
  
    Slider newslider = new Slider(0, 2, 1.5);
      
    newslider.setShowTickMarks(true);
   
    newslider.setShowTickLabels(true);
   
    newslider.setMajorTickUnit(0.50f);
   
         
    newslider.setBlockIncrement(0.1f);  
        Scene scn = new Scene(pane, 600, 400);
        pane.getChildren().add(newslider);
        primaryStage.setScene(scn);  
        primaryStage.setTitle("JavaFx Slider Example");  
        primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Slider with tick marks in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.Slider, javafx.scene.Group.

Then we have created one class named SliderUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a slider with tick marks, Group is created which is then passed to the Scene class object.

The Slider is created with its constructor having min value, max value, and current value passed into it. setShowTickMarks() method is used to set values for tick marks. setShowTickLabels() method is used to set labels for values in Slider. setMajorTickUnit() is used to set the values of major ticks. setBlockIncrement() method is used to set interval for blocks for increment.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with the title, “ JavaFx Slider Example ”. Also, it displays Slider with 0.5 intervals and shows the current value at 1.5 with min and max value set to 0 and 2 respectively.

JavaFX Slider

JavaFX Vertical Slider:

Example:

import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Group;
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 SliderUI extends Application {  


    @Override  
    public void start(Stage primaryStage) throws Exception 
    {  
    Label newlabel = new Label("Choose level you want");
      
        Slider newslider = new Slider();
  
       
        newslider.setMin(0);
        newslider.setMax(60);
        newslider.setValue(40);
        newslider.setBlockIncrement(10);


  
        newslider.setShowTickLabels(true);
        newslider.setShowTickMarks(true);
        newslider.setOrientation(Orientation.VERTICAL);


  
      
  
        VBox root = new VBox();
  
        root.setSpacing(10);
        root.getChildren().addAll(newlabel, newslider);
        Scene scn = new Scene(root, 400, 200);
        primaryStage.setScene(scn);  
        primaryStage.setTitle(" JavaFx Slider with tick marks Example ");  
        primaryStage.show();  
        }
    public static void main(String[] args) {
        Application.launch(args);
    }
}

Output:

In order to create the Vertical Slider with tick marks in JavaFX, we have to import all the required libraries such as javafx.application.Application, javafx.scene.Scene, javafx.stage.Stage, javafx.scene.control.Slider, javafx.scene.Group, javafx.scene.control.Label, javafx.scene.layout.VBox, and javafx.scene.paint.Color.

Then we have created one class named SliderUI extending the Application class. Also, we have to override the start method to provide implementation details. This method creates an object of Stage as primaryStage. For the container to hold a vertical slider with tick marks, Group is created which is then passed to the Scene class object.

The Slider is created with its constructor. The setMin(), setMax(), and setValue() method is used to set min, max, and current value. setShowTickMarks() method is used to set values for tick marks. setShowTickLabels() method is used to set labels for values in Slider. setOrientation(Orientation.VERTICAL) is used to display vertical slider. setBlockIncrement() method is used to set interval for blocks for increment.

The stage is prepared, the title is set and the show() method is called to display output. In order to run the application, the launch(args) method is called in the main() method. In output Frame like container is displayed with a title, “ JavaFx Slider with tick marks Example ". Also, it displays Slider and shows the current value at 40 with min and max value set to 0 and 60 respectively.

JavaFX Slider

Related Topics

JavaFX Line Chart

In the JavaFX application, in order to create a Line Chart, LineChart class is used. The LineChart allows us to plot markers point according to the entities on number axes....

4 minutes read.

JavaFX TilePane

In the JavaFX application, in order to set TilePane as a layout, the TilePane class is used. The TilePane layout allows us to arrange the components in the same sized...

4 minutes read.

JavaFX ScrollBar

In the JavaFX application, in order to scroll over a range of values, ScrollBar is used. It is used to view long page contents. In order to use ScrollBar in...

4 minutes read.

JavaFX Reflection

In the JavaFX application, in order to apply various reflection effect, we use this class. It has the ability to create the reflection of the given object. This effect adds...

4 minutes read.

JavaFX Polygon

In the JavaFX application, in order to draw a polygon, the Polygon class is used. The Polygon allows us to join any number of points in the space. All methods...

4 minutes read.

JavaFX Drop Shadow

In the JavaFX application, in order to apply various Drop Shadow effect, we use this class. It has the ability to create the drop shadow of the given object. This...

4 minutes read.

JavaFX Ellipse

In the JavaFX application, in order to draw an Ellipse, the Ellipse class is used. The Ellipse allows us to join any points in the space to draw the unique...

7 minutes read.

JavaFX Shear

In the JavaFX application, in order to apply various shear effect, we use this class. It has the ability to shear the given object along the x or y axis....

3 minutes read.

JavaFX BorderPane

In the JavaFX application, in order to set Border Pane as a layout, the BorderPane class is used. The BorderPane layout allows us to arrange the components in the left,...

4 minutes read.

JavaFX Event Handling

JavaFX gives way to handle various events in web application, desktop applications and graphical applications. In the JavaFX application, in order to allow user to interact with application various events...

4 minutes read.

JavaFX Menu

In the JavaFX application, in order to create a menu, menu items, and menu bar, Menu, MenuItem, and MenuBar class is used. The menu allows us to choose options among...

4 minutes read.

JavaFX Color Adjust

In the JavaFX application, in order to apply various color adjust effect, we use ColorAdjust class. It has the ability to apply various color adjust effect such as hue, saturation,...

5 minutes read.

JavaFX GridPane

In the JavaFX application, in order to set Grid Pane as a layout, the GridPane class is used. The GridPane layout allows us to arrange the components in the form...

4 minutes read.

JavaFX Scatter Chart

In the JavaFX application, in order to create Scatter Chart, the ScatterChart class is used. The Scatter Chart allows us to plot data along the x and y axis of...

4 minutes read.

JavaFX Inline Styles

In the JavaFX application, in order to apply various Inline css effect we will use the various css classes available in the JavaFX. It is used to apply the various...

4 minutes read.

JavaFX Glow

In the JavaFX application, in order to apply various glow effect, we use Glow class. It has the ability to glow the colors of the shape. All methods needed for...

4 minutes read.

JavaFX Shadow

In the JavaFX application, in order to apply various Shadow effect from a single source, we use this class. It has the ability to create the shadow of the given...

3 minutes read.

JavaFX Selectors

In the JavaFX application, in order to apply various css effect, we use the various css classes available in the JavaFX. This is generally used to enhance the appearance property...

4 minutes read.

JavaFX Slider

In the JavaFX application, in order to slide over a range of data having values, Slider is used. It provides a pane with a Slider to move over a range...

4 minutes read.

JavaFX Layouts

Layouts in the JavaFX application are treated as a container for different UI elements such as Button, Label, TextArea, etc. Layouts are used as parent container. The layout is used...

3 minutes read.