Alexander Gryzlov
Alexander Gryzlov

Reputation: 933

Closing stage after specific event

How do I close a stage in JavaFX 2 after some specific external event has occurred? Suppose I have a stage with a simple progress bar that is filled up by a Task (borrowed from another answer):

Task<Void> task = new Task<Void>(){
                @Override
                public Void call(){
                    for (int i = 1; i < 10; i++)    {
                        try {
                            Thread.sleep(200);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                        System.out.println(i);
                        updateProgress(i, 10);
                    }
                return null;                
                }
            };

How do I close the window automatically (and open the next one) after the Task is done and the ProgressBar is filled to 100%?

Upvotes: 1

Views: 331

Answers (1)

Alexander Kirov
Alexander Kirov

Reputation: 3654

Before return null; you can add

Platform.runLater(
    new Runnable() {
        public void run() {
            stage.close();
        }
    }
);

or

progressBar.progressProperty().addListener(new ChangeListener<Number>(){
//add checking, that progress is >= 1.0 - epsilon
//and call stage.close();
})

The first is better. But note, that task is done on a separate thread. so you should put a request on stage.close() on JFX thread using special call.

Also, jewelsea provides links on stage closing questions in comment to the question.

Upvotes: 3

Related Questions