How to create a JavaFX slider?

JavaObject Oriented ProgrammingProgramming

JavaFX provides a class known as Slider, this represents a slider component that displays a continuous range of values. This contains a track on which the numerical values are displayed. Along the track, there is a thumb pointing to the numbers. You can provide the maximum, minimum and initial values of the slider. 

To create a slider you need to instantiate the Slider class, set the required properties and, add it to the scene.

Example

import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;         
public class SliderExample extends Application {
   public void start(Stage stage) {      
      //Setting the slider
      Slider slider = new Slider(0, 500, 0);      
      slider.setShowTickLabels(true);
      slider.setShowTickMarks(true);
      slider.setMajorTickUnit(100);
      slider.setBlockIncrement(50);     
      //Creating a VBox
      VBox vbox = new VBox();
      vbox.setPadding(new Insets(50, 50, 50, 50 ));
      vbox.setSpacing(150);
      vbox.getChildren().addAll(slider);      
      //Preparing the scene
      Scene scene = new Scene(vbox, 595, 150); 
      stage.setTitle("Slider Example");
      stage.setScene(scene);
      stage.show();
   }     
   public static void main(String args[]){
      launch(args);
   }
}

Output:

raja
Published on 09-Apr-2020 09:12:45
Advertisements