Friday 22 June 2018

JavaFX: Slider: Create Slider widget

Slider class provides below constructors to create Slider widget.

public Slider()
public Slider(double min, double max, double value)
min : Specifies the minimum value of the slider.
max : Specifies the maximum value of the slider
value : Specifies the current value of the slider.

Example 1
Slider slider1 = new Slider();
slider1.setShowTickLabels(true);
slider1.setShowTickMarks(true);
slider1.setMin(0);
slider1.setMax(100);
slider1.setValue(50);
slider1.setMajorTickUnit(50);
slider1.setMinorTickCount(5);
slider1.setBlockIncrement(10);

Example 2
Slider slider2 = new Slider(0, 100, 25);
slider2.setShowTickLabels(true);
slider2.setShowTickMarks(true);

Find the below working application.

SliderApp.java
package com.sample.demos;

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

public class SliderApp extends Application {

 @Override
 public void start(Stage primaryStage) throws Exception {
  Slider slider1 = new Slider();
  slider1.setShowTickLabels(true);
  slider1.setShowTickMarks(true);
  slider1.setMin(0);
  slider1.setMax(100);
  slider1.setValue(50);
  slider1.setMajorTickUnit(50);
  slider1.setMinorTickCount(5);
  slider1.setBlockIncrement(10);

  Slider slider2 = new Slider(0, 100, 25);
  slider2.setShowTickLabels(true);
  slider2.setShowTickMarks(true);

  HBox hBox = new HBox(50, slider1, slider2);

  Scene scene = new Scene(hBox);

  primaryStage.setScene(scene);

  primaryStage.setTitle("Slider widget Example");
  primaryStage.setWidth(900);
  primaryStage.setHeight(500);
  primaryStage.show();
 }

}

TestFX.java
package com.sample.demos;

import javafx.application.Application;

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


Let me explain the methods of slider widget in brief.

slider1.setShowTickLabels(true);
Display tick labels

slider1.setShowTickMarks(true);
Display tick marks

slider1.setMin(0);
Set the minimum value represented by the slider

slider1.setMax(100);
Set the maximum value represented by the slider.

slider1.setValue(50);
Set the current value of the slider.

slider1.setMajorTickUnit(50);
The distance between major tick marks.

slider1.setMinorTickCount(5);
Number of minor ticks between major ticks.

slider1.setBlockIncrement(10);
This specifies the distance a slider moves, when user clicks on the track.




Previous                                                 Next                                                 Home

No comments:

Post a Comment