0

Hello I have app with alert and after no response from user I want to refresh alert/show it again and for some reason I don't see second alert it's empty like:

enter image description here

I am closing alert if it's showing so I don't know why next alert is empty. package application;

//imports

public class Main extends Application{

    int number = 50;
    public static void main(String[] args) {
        launch(args);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        alertMethod();
    }


    private void alertMethod() {
        number += number + 10;
        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Title");
        alert.setHeaderText("Number " + number);
        alert.setContentText("Choose your option.");     

        ButtonType buttonTypeOne = new ButtonType("Yes");
        ButtonType buttonTypeCancel = new ButtonType("No", ButtonBar.ButtonData.CANCEL_CLOSE);
        alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);

        Thread newThread = new Thread(new Runnable() {
            @Override
                public void run() {
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException ex) {
                        Thread.currentThread().interrupt();
                    }

                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            if(alert.isShowing()) {
                                alert.close();
                            }
                            alertMethod();
                        }
                        });
                    }
            });
            newThread.start();
        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == buttonTypeOne){
            System.out.println("Pressed Yes");
         } else if (result.get() == buttonTypeCancel) {
            System.out.println("Pressed No");
         }
    }
}
Boris
  • 22,667
  • 16
  • 50
  • 71
  • I don't understand your problem. `alert.showAndWait()` should make the program unresponsive until the user closes the `Alert`. Why the `Thread`? – SedJ601 Feb 06 '18 at 15:13
  • I wanted after 2 seconds without action to close alert and show again new. – Zapp Scierwx Feb 06 '18 at 15:24

1 Answers1

0

This is just a guess at what I think you want. Your problem is not very clear. This app uses @Sergey Grinev code from here. It updates the Alert every two seconds.

//imports
import java.util.Optional;
import javafx.animation.KeyFrame;
import javafx.animation.Timeline;
import javafx.application.Application;
import static javafx.application.Application.launch;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.Alert;
import javafx.scene.control.ButtonBar;
import javafx.scene.control.ButtonType;
import javafx.stage.Stage;
import javafx.util.Duration;

public class Main extends Application
{

    int number = 50;

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

    @Override
    public void start(Stage primaryStage) throws Exception
    {
        alertMethod();
    }

    private void alertMethod()
    {

        Alert alert = new Alert(Alert.AlertType.CONFIRMATION);
        alert.setTitle("Title");
        alert.setHeaderText("Number " + number);
        alert.setContentText("Choose your option.");

        ButtonType buttonTypeOne = new ButtonType("Yes");
        ButtonType buttonTypeCancel = new ButtonType("No", ButtonBar.ButtonData.CANCEL_CLOSE);
        alert.getButtonTypes().setAll(buttonTypeOne, buttonTypeCancel);

        Timeline twoSecondsWonder = new Timeline(new KeyFrame(Duration.seconds(2), new EventHandler<ActionEvent>()
        {
            @Override
            public void handle(ActionEvent event)
            {
                number = number + 10;
                alert.setAlertType(Alert.AlertType.CONFIRMATION);
                alert.setTitle("Title -> " + number);
                alert.setHeaderText("Number " + number);
                alert.setContentText("Choose your option.");
            }
        }));
        twoSecondsWonder.setCycleCount(Timeline.INDEFINITE);
        twoSecondsWonder.play();

        Optional<ButtonType> result = alert.showAndWait();
        if (result.get() == buttonTypeOne) {
            System.out.println("Pressed Yes");
        }
        else if (result.get() == buttonTypeCancel) {
            System.out.println("Pressed No");
        }
    }
}
SedJ601
  • 12,173
  • 3
  • 41
  • 59
  • 1
    This is what I exactly meant. Looks like it's better to user timeline instead of thread and sleep. Thanks man! – Zapp Scierwx Feb 06 '18 at 15:29
  • If the answer is correct, accept it as correct. Select the "check" symbol under the 0 and down arrow to the left of the answer. – SedJ601 Feb 06 '18 at 15:33