• Post Reply Bookmark Topic Watch Topic
  • New Topic

gui and thread  RSS feed

 
jonathan Greens
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!
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rob Spoor
Sheriff
Posts: 21052
85
Chrome Eclipse IDE Java 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: 21052
85
Chrome Eclipse IDE Java 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?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!