Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Get Programming with Java (MEAP only) this week in the Beginning Java forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Bear Bibeault
  • Knute Snortum
  • Liutauras Vilda
Sheriffs:
  • Tim Cooke
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Ron McLeod
  • Ganesh Patekar
  • salvin francis
Bartenders:
  • Tim Holloway
  • Carey Brown
  • Stephan van Hulst

JavaFX does not launch GIF in a separate thread  RSS feed

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Salute.

I apologize in advance for a lot of text, but I come back to this problem the second day and tried several solutions, plus I did not receive a working answer on stackoverflow.  So - I hope for help here.

Essence: on the GUI (JavaFX) there is a button that should launch two processes: the main program code (about 2 seconds) and the wait gif, which runs on top of the GUI.  Accordingly, when the code is executed - the GIF is completed.

What I have done now:
The button's action method blocks the isStarted flag, which gets true when the button is pressed and false after the completion of the main code.  In the main, before launching the GUI, I created a Task, which checks the flag in an infinite loop and, as soon as it gets true, it starts my gif with a separate showGif() method.

Main:



Button:

 
showGif():





Result: the stream starts before the main code, the cycle is spinning, the GIF does not open  - showGif() method stops at pane.setCenter(hb) .  I added Platform.runLater() before calling the showGif() method in Task:




Now the cycle works completely, but the showGif() method is executed only after all the main code.  GIF opens, but no longer needed.


I also tried the call to the showGif() method via Platform.runLater() directly in the button method, without any cycles, flags and Tasks - the result was the same (the GIF starts, but at the end of the program.)  I added three seconds of delay to the main code, in case the GIF just didn't have enough time - the result is the same.

Tell me what am I doing wrong?  I don't see the options.
 
Marshal
Posts: 5637
147
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, sorry for the late response.  Do you specifically want a certain GIF to show you're task is busy, or can you use a "busy" cursor?  If the latter, your pseudocode could be as simple as:
  • set cursor to busy
  • start main task
  • set cursor to normal
  •  
    Knute Snortum
    Marshal
    Posts: 5637
    147
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Hmm... Looks like that only works off the JavaFX thread.  Maybe try a Service?
     
    Knute Snortum
    Marshal
    Posts: 5637
    147
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    So here's what I figured out using the cursor.  I bet it could be adapted to use a GIF.

  • set the cursor to WAIT
  • create a Runnable
  • inside the Runnable, do...
  • start your task using Platform.runAfter()
  • set cursor to NORMAL
  • put Runnable in a Thread
  • run the Thread


  • Here's what it looks like:
     
    Andrey Mironov
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote:Do you specifically want a certain GIF to show you're task is busy...?


    Yep, GIF should run over the program window while the program code is being executed. Like this:


    I followed your advice and wrapped 'Platform.runLater (() -> showGif ())' into Runnable. I tried to place it directly in the Event Handler - the result is the same, the GIF runs only after all the main code. Although, logically, it should be launched in parallel - a separate thread is running.



    Then I again decided to use my previous approach with the launch in main of an infinite loop that checks the 'isStarted' status of the button. Instead of Task set Runnable. Did not help. Again, first the code - then gif.



    How to make them work at the same time?
     
    Knute Snortum
    Marshal
    Posts: 5637
    147
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    The execution goes like this:

  • create one Runnable that contains...
  • the long task in a Platform.runLater() method
  • then, a second Platform.runLater() removing the GIF

  • Platform.runLater() does "run later," but the tasks to run later are put into the queue one after another.  That's why you can have two "run laters" and be guaranteed they will run one after the other.
     
    Andrey Mironov
    Greenhorn
    Posts: 3
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Knute Snortum wrote:Platform.runLater() does "run later," but the tasks to run later are put into the queue one after another.  That's why you can have two "run laters" and be guaranteed they will run one after the other.



    A solution with two Platform.runLater() really worked. It is a pity that it did not occur to me before.

    Thanks for the help!
     
    Knute Snortum
    Marshal
    Posts: 5637
    147
    Chrome Eclipse IDE Java Postgres Database VI Editor
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    My pleasure.
     
    Do you pee on your compost? Does this tiny ad?
    Programmatically Create PDF Using Free Spire.PDF with Java
    https://coderanch.com/wiki/703735/Programmatically-Create-PDF-Free-Spire
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!