JavaFX Slider tutorials

0
The JavaFX Slider is used to display and interact with a range of numeric value. And Thumb slides in the range of numeric value of both sides.

It consists a track, draggable thumb, tick label and tick marker.

Constructors:

  • 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.

Creating a slider :

Slider slid = new Slider();

And use these methods for well defining :
  • setMin() for setting minimum value of slider.
  • setMax() for setting maximum value of slider.
  • setValue() to set value of the slider.
  • setShowTickLabels(true) to show the tick labels.
  • setShowTickMarks(true) to show the tick marks.
  • setBlockIncrement() to set block increment.

Example of slider:


package javafxtuts;
import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Javafxtuts extends Application {

  public static void main(String[] args) {
     Application.launch(args);
  }

  @Override
  public void start(Stage stage) {
      stage.setTitle("Javafxtuts.com");
      Label lb=new Label(" Music Volume ");
      Slider slid = new Slider();
      slid.setMin(0);
      slid.setMax(100);
      slid.setValue(40);
      slid.setShowTickLabels(true);
      slid.setShowTickMarks(true);
      slid.setBlockIncrement(10);
      VBox root =new VBox();
      root.setPadding(new Insets(40, 10, 10, 10));
      root.getChildren().addAll(slid);
        Scene scene = new Scene(root, 250, 200);
        stage.setScene(scene);
        stage.show();
  }
}

Output :

Explanation of slider example :

  • Create slider as

    Slider slid = new Slider();

  • slid.setMin(0);
    It set minimum value ‘0’ of slider.
  • slid.setMax(100); it set max value ‘100’ of slider.
  • slid.setShowTickLabels(true); to show the tick labels.
  • slid.setShowTickMarks(true); to show the tick marks.
Share.

Leave A Reply