JavaFX – Spinner


JavaFX – Spinner



”;


A Spinner is a UI control that allows the user to select a value from a predefined range or a ordered sequence. It can be either editable or non-editable. If it is editable, the user can type in a value otherwise not. It also provides up and down arrows so that a user can step through the values of sequence. The figure below illustrates a spinner −


Spinner

Creating a Spinner in JavaFX

In JavaFX, the spinner is created by instantiating a class named Spinner. This class belongs to the package javafx.scene.control. Some of the widely used constructors of the Spinner class are listed below −

  • Spinner() − It is used to create an empty spinner.

  • Spinner(double minVal, double maxVal, double initialVal) − It creates a new spinner with the specified minimum, maximum and initial values.

  • Spinner(double minVal, double maxVal, double initialVal, double valToStepBy) − It is used to construct a new spinner with the specified minimum, maximum and initial values along with the increment amount.


While creating a spinner in JavaFX, our first step would be instantiating the Spinner class by using any of the above mentioned constructors. The datatype of minimum, maximum and initial values can be either double or integer. Next, define a layout pane, such as Vbox or Hbox by passing the Spinner object to its constructor. Lastly, set the scene and stage to display the spinner on the screen.

Example

The following JavaFX program demonstrates how to create a spinner in JavaFX application. Save this code in a file with the name JavafxSpinner.java.


import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
public class JavafxSpinner extends Application {
   @Override
   public void start(Stage stage) {
      // creating a label 
      Label newlabel = new Label("Sample Spinner: ");
      // creating spinner and setting min, max, initial value
      Spinner newSpinner = new Spinner(0, 100, 25); 
      // vbox to hold spinner
      VBox vbox = new VBox(newlabel, newSpinner);
      vbox.setAlignment(Pos.CENTER); 
      // creating stage and scene
      Scene scene = new Scene(vbox, 400, 300);
      stage.setScene(scene);
      stage.setTitle("Spinner in JavaFX");
      stage.show();
   }
      public static void main(String[] args) {
      launch(args);
   }
}

To compile and execute the saved Java file from the command prompt, use the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxSpinner.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls JavafxSpinner

Output

When we execute the above code, it will generate the following output.


Spinner Output

Setting the Size of Spinner

To set the size of a spinner, we can use setPrefSize() method. It is a built-in method which accepts heigth and width as a parameter.

Example

In the following example, we are going to create a Spinner of the specified size in JavaFX application. Save this code in a file with the name DemoSpinner.java.


import javafx.application.Application;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
import javafx.scene.control.Label;
import javafx.geometry.Pos;
public class DemoSpinner extends Application {
   @Override
   public void start(Stage stage) {
      // creating labels for spinner
      Label newlabel = new Label("Enter Date of Birth: ");
      Label setYear = new Label("Year: ");
      Label setMonth = new Label("Month: ");
      Label setDay = new Label("Day: ");
      // creating spinners and setting sizes
      Spinner year = new Spinner(1998, 2020, 2000);
      year.setPrefSize(65, 25);
      Spinner month = new Spinner(1, 12, 1);
      month.setPrefSize(60, 25);
      Spinner day = new Spinner(1, 31, 1);
      day.setPrefSize(60, 25);
      // HBox to hold labels and spinners
      HBox box1 = new HBox();
      box1.setPadding(new Insets(15, 12, 15, 12));
      box1.setSpacing(10);
      box1.getChildren().addAll(setYear, year, setMonth, month, setDay, day);
      // VBox to hold HBox and Label
      VBox box2 = new VBox();
      box2.setAlignment(Pos.CENTER); 
      box2.setPadding(new Insets(15, 12, 15, 12));
      box2.setSpacing(10);
      box2.getChildren().addAll(newlabel, box1);
      // creating scene and stage
      Scene scene = new Scene(box2, 400, 400);
      stage.setScene(scene);
      stage.setTitle("Spinner in JavaFX");
      stage.show();
   }
      public static void main(String[] args) {
      launch(args);
   }
}

Compile and execute the saved Java file from the command prompt by using the following commands −


javac --module-path %PATH_TO_FX% --add-modules javafx.controls DemoSpinner.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DemoSpinner

Output

On executing the above code, it will generate the following output.


Spinner Output2

Advertisements

”;

Leave a Reply

Your email address will not be published. Required fields are marked *