Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

150
Vistas
JavaFX - Show Alert Dialog within ChangeListener event

I want to show a alert dialog when the valueProperty of a spinner is changed. Therefore i've added a ChangeListener to my valueProperty:

this.spinnerColumns.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(2,20));

this.spinnerColumns.getValueFactory().valueProperty().addListener((observable, oldValue, newValue) -> {

  if(oldValue > newValue) {
    // Ask for permission
    System.out.println("Ask for permission");
    Alert alert = new Alert(Alert.AlertType.WARNING);
    alert.setTitle("Error");
    alert.setHeaderText("Ask something...");
    alert.showAndWait();

    if(alert.getResult() == ButtonType.OK) {
      // do something
    }
  }
});

This code is not working properly. When the Alert is showing, the spinner keeps decreasing his value until the min value is reached. Some suggestions how to do this?

over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

It's difficult to stop spinning without using reflection as far as I know... Try this code.

if(oldValue > newValue) {

    try {
        Skin<?> skin = this.spinnerColumns.getSkin();
        Object behavior = skin.getClass().getMethod("getBehavior").invoke(skin);
        behavior.getClass().getMethod("stopSpinning").invoke(behavior);
    } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
        e.printStackTrace();
        return; // If refleciton failed, do nothing
    }

    // Ask for permission
over 4 years ago · Santiago Trujillo Denunciar

0

It seems like Bug JDK-8089332 and is similar to Bug JDK-8252863. The workaround there leads to:

this.spinnerColumns.setValueFactory(new SpinnerValueFactory.IntegerSpinnerValueFactory(2, 20));

this.spinnerColumns.getValueFactory().valueProperty().addListener((observable, oldValue, newValue) -> {
    Node increment = spinnerColumns.lookup(".increment-arrow-button");
    if (increment != null) {
        increment.getOnMouseReleased().handle(null);
    }

    Node decrement = spinnerColumns.lookup(".decrement-arrow-button");
    if (decrement != null) {
        decrement.getOnMouseReleased().handle(null);
    }

    if (oldValue > newValue) {
        // Ask for permission
        System.out.println("Ask for permission");
        Alert alert = new Alert(Alert.AlertType.WARNING);
        alert.setTitle("Error");
        alert.setHeaderText("Ask something...");
        alert.showAndWait();

        if (alert.getResult() == ButtonType.OK) {
            // do something
        }
    }
});
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda