Win a copy of Svelte and Sapper in Action this week in the JavaScript 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
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

gui and thread

 
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I want to accomplish the following: user clicks to close a JFrame, then a JDialog popups up display an indeterminate JProgressBar for 3 seconds, then close the popup, close the JFrame. What's the best practice here? I tried this. But the popup dialog doesn't show the progressbar. I'm guessing that I'm not doing the threading correctly. What's wrong with my code?


thanks!
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Sheriff
Posts: 21999
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Craig, are you really doing user interface coding in a thread that's not the event dispatcher thread?

javax.swing.SwingWorker would be more useful I think:
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thank you both for the help.
Craig, I tried your code and it works fine except that the when user clicks to close the window, the window closes. and then the progress dialog will be displayed for 3 seconds before disappearing. Is there a way to delay the window from closing for 3 seconds as well?

Rob, I tried your SwingWorker code and the progress dialog closes rightaway without waiting for 3 seconds. any idea why?

thanks
 
jonathan Greens
Ranch Hand
Posts: 139
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rob, nevermind, your code works fine. The key is to make the progress dialog modal, otherwise when I close the window, the dialog gets closed rightaway too.
 
Rob Spoor
Sheriff
Posts: 21999
107
Eclipse IDE Spring VI Editor Chrome Java Ubuntu Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct. Non-modal dialogs will not block on their setVisible method, so the windowClosing method finishes and the window is closed.

By the way, I'm sorry for the compiler errors in my code; it was just quickly copied from Craig's code and modified a bit; moving code around failed to rename some stuff

And I noticed the dialog does not center around the frame, but a simple call to dialog.setLocationRelativeTo(frame) will do that for you.


If you want to remove the close button etc of the dialog, you can call setUndecorated(true) on it. You should call this before pack() because it will fail for realized windows. Check the API for more info.
[ October 24, 2008: Message edited by: Rob Prime ]
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there a way to delay the window from closing for 3 seconds as well?
 
I'm gonna teach you a lesson! Start by looking at this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic