×

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 PasswordField

In the JavaFX application, to get the user information and password as input from the user, a form is created using textfields and password fields which allows the user to...

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 Transformation

In the JavaFX application, in order to apply various transformation effects, we use Transform class. It has the ability to create the transformation on the given object. This transformation changes...

3 minutes read.

JavaFX Gaussian Blur

In the JavaFX application, in order to apply various Gaussian blur effect, we use GaussianBlur class. It has the ability to apply blur effect on the given object. All methods...

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 Bloom

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

4 minutes read.

JavaFX Bubble Chart

In the JavaFX application, in order to create Bubble Chart, the BubbleChart class is used. The Bubble Chart allows us to plot three dimensional data. All methods needed for this...

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 Inner Shadow

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

4 minutes read.

JavaFX Lighting

In the JavaFX application, in order to apply various Lighting effect from a single source, we use this class. It has the ability to lighten a node from a given...

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 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 Line

In the JavaFX application, in order to draw a line, the Line class is used. The line allows us to join any two points with x and y coordinates in...

3 minutes read.

JavaFX Pause Transition

In the JavaFX application, in order to generate the pause transition, we have to apply pause transition on any particular shape. It has the ability to take a particular pause...

4 minutes read.

JavaFX HyperLink

In the JavaFX application, in order to access and use webpages, hyperlinks are used. In HTML anchor tags are used in the same way in JavaFX hyperlinks are used. To...

4 minutes read.

JavaFX Event Filters

In JavaFX, in order to handle the event which are created by any of the mouse action, keyboard action, rotate action, scroll action Event filters are used. The event filters...

3 minutes read.

JavaFX Label

The UI Control Label in a JavaFX is used to display the simple text. To use Label in JavaFX application javafx.scene.control.The label class is used. We can place Labels on...

3 minutes read.

JavaFX - Basic Structure of Application

Every JavaFX application is mainly divided into three main components namely Scene, Stage, and nodes. For using these components we need to import javafx.application.Application class in every javafx application. Life...

4 minutes read.

JavaFX Architecture

JavaFX architecture consists of many built-in components such as JavaFX public API, Scene Graph, Quantum Toolkit, Prism, Glass windowing toolkit, web view, media engines. These components help to develop rich...

3 minutes read.

JavaFX TextField

In the JavaFX application, to get the user information as input from the user, a form is created using textfields which allows the user to enter input for specific fields....

4 minutes read.