• Post Reply Bookmark Topic Watch Topic
  • New Topic

Modal Wait-Dialog in FX?  RSS feed

 
Mario Schwaiger
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm pretty new to JavaFX and would like to establish a waiting window (modal) for an operation which is going to consume more time.

Strangely, when I use the code like that the window only opens AFTER:



Other Variants like are ending up the same way and just using some text-information like are having the same result: They give the information after the operation was finished.

Doing some research made me switch to a thread instead, coming up with this and the class LongOperation o, which implements Runnable.



Haven't really worked with threads for a while and seems mixing it with FX is not something I understand too much of :/
Help is highly appreciated, thank you
 
Knute Snortum
Sheriff
Posts: 4289
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I haven't done this much either, but I have successfully implemented a progress bar while the task was working.

I think the trick is to get your thread off of the main JavaFX thread.  Working with threads directly is not recommended as there are many library classes that can help you.  I used a javafx.concurrent.Task because I had a return value, but maybe you don't need this.

Have you read the Oracle documentation for concurrency?  It's de rigueur for the topic.

This is how I implemented the progress meter.  It don't all apply to your situation, but maybe you can glean something from it.  If you want to see the context, it on this GitHub page, in the class ScrabbleWords
 
Mario Schwaiger
Greenhorn
Posts: 18
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help!

Made my program run =)

 
John Damien Smith
Ranch Hand
Posts: 357
22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You really shouldn't be calling thread.join() on the JavaFX application thread.  This will pause the JavaFX application thread, effectively stopping the UI from responding or updating until the thread which you joined completes.  This means that the user cannot normally close your application by clicking the close icon in a window, but would need to do some sort of OS level kill to really kill it (at least until the joined thread completes).

Instead, you should use a listener to respond when something completes.

I'd advise putting an ProgressIndicator (or ProgressBar) inside your alert dialog so that the user has some idea that the application is still processing.  If you can report actual progress to the progress indicator, I advise doing that.  But sometimes working out actual progress is difficult, in which case you can just set the progress indicator to indeterminate, so it just spins rather than reports the amount of progress, then at least the user has some idea that something is going on even if they don't know how long it may take.

The third party ControlsFX library has a ProgressDialog, so this would probably be the best thing to use for your implementation.  An example on StackOverflow demonstrates how to use this class:
  https://stackoverflow.com/questions/27866432/controls-fx-progress-dialog-modality-alert-modality

When running the operation, it is best to use a Task or Service, as recommended by Knute.  Those classes make it easier to program a multi-threaded app in JavaFX and the javadoc for them has lots of useful examples for various scenarios you may wish to cater for.  Even if your process doesn't return a result, you can just use a Task<Void> and return null from the task, the task is still useful as it provides hooks and listeners for responding to state changes on the task and also adds some framework to make it easier to work with threads and relate values back to the GUI in a thread safe manner.

When you have a long running process, it is nice to give the user an option to initiate a graceful cancellation or shutdown of the process.

When you define the alert and want to make it modal, you should initOwner of the alert dialog to the parent stage, that way the windowing system knows which window to block.  In the case that you make the alert window modal (for the default application modal setting for an alert it won't matter, but I think its always good practice to have the owner defined).

So, I'd recommend something like this:



 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!