Dennis von Eich February 2016

Alert message for oversteping maximum integer in spinner

anzahlLED.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(1, 3, 1));
        ChangeListener<Integer> ledListener = new ChangeListener<Integer>() {
            @Override
            public void changed(ObservableValue<? extends Integer> observable, Integer oldValue, Integer newValue) {
                if (newValue == 1 && oldValue == 2) {
                    secondLEDCombo.setDisable(true);
                    secondLEDTextUf.setDisable(true);
                    secondLEDTextIf.setDisable(true);
                    if (schaltung.getSelectionModel().getSelectedItem().equals("Parallelschaltung")) {
                        //
                    } else if (schaltung.getSelectionModel().getSelectedItem().equals("Reihenschaltung")) {
                        secondLEDTextUf.setText(null);
                        secondLEDTextIf.setText(null);
                    }
                }
                if (newValue == 2 && oldValue == 1) {
                    secondLEDCombo.setDisable(false);
                    secondLEDTextUf.setDisable(false);
                    if (schaltung.getSelectionModel().getSelectedItem().equals("Parallelschaltung")) {
                        secondLEDTextIf.setDisable(false);
                    } else if (schaltung.getSelectionModel().getSelectedItem().equals("Reihenschaltung")) {
                        secondLEDTextIf.setText(firstLEDTextIf.getText());
                    }
                }
                if (newValue == 2 && oldValue == 3) {
                    thirdLEDCombo.setDisable(true);
                    thirdLEDTextUf.setDisable(true);
                    thirdLEDTextIf.setDisable(true);
                    if (schaltung.getSelectionModel().getSelectedItem().equals("Parallelschaltung")) {
                        //
                    } else if (schaltung.getSelectionModel().getSelectedItem().equals("Reihenschaltung")) {
                        secondLEDTextIf.s        

Answers


James_D February 2016

This isn't very robust, but you can use a lookup to get the increment button, and register a listener with it. If you register an event filter for mouse pressed, it catches it before the value is changed.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Alert;
import javafx.scene.control.Spinner;
import javafx.scene.control.Alert.AlertType;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

public class SpinnerTest extends Application {

    @Override
    public void start(Stage primaryStage) {
        Spinner<Integer> spinner = new Spinner<>(1, 3, 1);
        Scene scene = new Scene(new StackPane(spinner), 180, 80);

        spinner.applyCss();
        spinner.lookup(".increment-arrow-button").addEventFilter(MouseEvent.MOUSE_PRESSED, e -> {
            if (spinner.getValue().intValue() == 3) {
                Alert alert = new Alert(AlertType.INFORMATION);
                alert.setContentText("Maximum is 3");
                alert.showAndWait();
            }
        });

        primaryStage.setScene(scene);
        primaryStage.show();


    }

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

Post Status

Asked in February 2016
Viewed 3,888 times
Voted 10
Answered 1 times

Search




Leave an answer