0

How can I control arrow up and down button from Spinner?

I want to prevent situation when I click up/down button and the text is null/empty

Iks Ski
  • 370
  • 1
  • 5
  • 19

1 Answers1

0

OK, problem is solved by ManoDestra. Here is the solution - https://stackoverflow.com/a/36550660/5885019

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Spinner;
import javafx.scene.control.SpinnerValueFactory;
import javafx.scene.control.SpinnerValueFactory.IntegerSpinnerValueFactory;
import javafx.stage.Stage;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;

public class Spin extends Application {
    Spinner<Integer> spinner;

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

    @Override
    public void start(Stage aPrimaryStage) throws Exception {
        IntegerSpinnerValueFactory valueFactory = new IntegerSpinnerValueFactory(0, 10);
        spinner = new Spinner<>(valueFactory);
        spinner.setEditable(true);
        spinner.valueProperty().addListener((observableValue, oldValue, newValue) -> handleSpin(observableValue, oldValue, newValue));

        aPrimaryStage.setScene(new Scene(spinner));
        aPrimaryStage.show();
    }

    private void handleSpin(ObservableValue<?> observableValue, Number oldValue, Number newValue) {
        try {
            if (newValue == null) {
                spinner.getValueFactory().setValue((int)oldValue);
            }
        } catch (Exception e) {
            System.out.println(e.getMessage());
        }
    }
}
Community
  • 1
  • 1
Iks Ski
  • 370
  • 1
  • 5
  • 19