Spinner
In JavaFX, a Spinner is a control that allows users to select a value from a sequence of values. It usually has up and down arrows to increment or decrement the value. You can use it for numbers, dates, or even custom lists.
Example: Numeric Spinner
FXML (hello-view.fxml)
<?xml version="1.0" encoding="UTF-8"?>
<?import javafx.scene.control.Label?>
<?import javafx.scene.control.Spinner?>
<?import javafx.scene.layout.VBox?>
<VBox spacing="15" alignment="CENTER"
xmlns="http://javafx.com/javafx/17"
xmlns:fx="http://javafx.com/fxml/1">
<Label text="Select Quantity"/>
<Spinner fx:id="quantitySpinner"
prefWidth="100"/>
</VBox>
Controller (HelloController.java)
package org.example.demo;
import javafx.fxml.FXML;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
public class HelloController {
@FXML
private Spinner<Integer> quantitySpinner;
@FXML
public void initialize() {
// Set numeric values from 0 to 10, starting at 1, step 1
SpinnerValueFactory<Integer> valueFactory =
new SpinnerValueFactory.IntegerSpinnerValueFactory(0, 10, 1, 1);
quantitySpinner.setValueFactory(valueFactory);
}
}
Key Features of Spinner:
ValueFactory: Determines what kind of values the spinner will hold. Examples:
IntegerSpinnerValueFactory→ numeric integersDoubleSpinnerValueFactory→ decimal numbersListSpinnerValueFactory→ custom lists (e.g.,["Red", "Green", "Blue"])
Editable: You can allow users to type values manually using
spinner.setEditable(true).Increment/Decrement: Controlled by arrows or keyboard.
Range and Step: Configured in the
SpinnerValueFactory.