Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get make my program wait until JavaFX window has been closed before continuing?

I have a program that is displaying a barchart of results. I want to wait until the user closes the bar chart to continue to the next line of code which is asking if they want to enter new information for the chart.

Scene scene = BarGraph.getBarChart();
primaryStage.setScene(scene);
primaryStage.setTitle("Bar Chart");
primaryStage.show();

repeat = JOptionPane.showConfirmDialog(null, "Would you like to enter another number?");

What is happening is the scene for the bar chart will open, but nothing will be displayed and immediately JOptionPane pops up with the question. If I hit no, then the chart displays, but the program ends. If I hit yes, the program loops back to earlier dialog windows, but won't display the chart until the other dialog ends.

I want to wait to prompt the user until after they close the bar chart. I'm so frustrated at this point, that I'd even put an artificial pause like wait(5000) or something, though I have not been able to get that to work either.

I've read into this so much and have yet to find a solution that actually works. The closest question was this: JavaFX wait till stage has been closed

From what I read there is that I can't actually cause the program to wait, but can set actions to occur once the window is closed. But following the suggestions there just caused the stage to open and then immediately close. I've been down several other rabbit holes, but to no avail.

like image 637
Crislips Avatar asked Apr 27 '17 01:04

Crislips


People also ask

How do I close JavaFX program?

The static exit() method in the Platform class is the preferred way to programmatically end a JavaFX program. It is preferred to System. exit() because it cleanly shuts down the application thread and gives it an opportunity to clean up by calling the application's stop() method before terminating.

Which method does a JavaFX application need to override?

Note that the start method is abstract and must be overridden. The init and stop methods have concrete implementations that do nothing. Calling Platform. exit() is the preferred way to explicitly terminate a JavaFX Application.

Which JavaFX application lifecycle method?

Lifecycle of JavaFX Applicationstart() − The entry point method where the JavaFX graphics code is to be written. stop() − An empty method which can be overridden, here you can write the logic to stop the application. init() − An empty method which can be overridden, but you cannot create stage or scene in this method.


1 Answers

Try using Stage.showAndWait() instead of Stage.show().

The show() method returns immediately regardless of the modality of the stage. Use the showAndWait() method if you need to block the caller until the modal stage is hidden (closed). The modality must be initialized before the stage is made visible.

- JavaFX documentation for Stage

As James_D points out, you'll need to create another stage first, as using this on the primary stage will cause an error.

This method must not be called on the primary stage or on a stage that is already visible.

like image 185
Stevoisiak Avatar answered Sep 21 '22 10:09

Stevoisiak