• Post Reply Bookmark Topic Watch Topic
  • New Topic

Sychronize swing app with main

 
venkata madugundu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I am rather new to Swing. I have a java application which needs to pop up a dialog to capture certain user inputs. Till the dialog is closed, my caller method (say 'main') should block.



I tried to achieve that with SwingUtilities::invokeAndWait, but as I understand it only waits on Event Dispatch Thread. My main method is run in non EDT. So it obviously does not wait.
So is there any other simple and decent way without getting into the business of threading.

Thanks
Venkata Madugundu
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just make the dialog (JDialog) modal, and that should take care of the blocking for you.
 
venkata madugundu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:Just make the dialog (JDialog) modal, and that should take care of the blocking for you.


Wow. That worked. I tried JDialog before but didn't care to set its modal property to true. I was under the impression that JDialog itself is always modal. Thanks a ton. The code that worked...

 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My guess is that you'll want to pass a reference to a parent JFrame or other something similar via a parameter in your createAndShowGUI method. This way you can create the JDialog so that it is modal to this GUI component (check out the JDialog constructors for more on this). Best of luck and HTH.
 
venkata madugundu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
pete stein wrote:My guess is that you'll want to pass a reference to a parent JFrame or other something similar via a parameter in your createAndShowGUI method. This way you can create the JDialog so that it is modal to this GUI component (check out the JDialog constructors for more on this). Best of luck and HTH.


Even without a parent JFrame, the JDialog came up standalone and only when the dialog was closed, the control returned to the caller method, 'main'. This is what I needed. The code that I posted worked. Thanks again for your helpful hint.
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you still should supply a parent to the dialog.

as you stated it works OK, but run it again, then change focus to another program.
the dialog vanishes, and you have to alt-tab or minimize everything to find it.

if the dialog has a parent, you only need to click the parent's item in the taskbar for the dialog to reappear/regain focus
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
JOptionPane has several showXXX dialogs. Using them is much easier than creating your own dialogs.
 
venkata madugundu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Michael Dunn wrote:you still should supply a parent to the dialog.

as you stated it works OK, but run it again, then change focus to another program.
the dialog vanishes, and you have to alt-tab or minimize everything to find it.

if the dialog has a parent, you only need to click the parent's item in the taskbar for the dialog to reappear/regain focus


I understand what you are saying. Infact that ALT+TAB thing is annoying and at times it is hard to figure out that the dialog has gone back and is hiding :-). I will try and see if I can parent the JDialog with a frame. Thanks Michael.
 
venkata madugundu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maneesh Godbole wrote:JOptionPane has several showXXX dialogs. Using them is much easier than creating your own dialogs.


I actually needed a tree control and some set of buttons on the dialog. So I was not too sure if JOptionPane could serve the purpose. But anyways, thanks for your inputs Maneesh, and I will see if JOptionPane can fit in my requirements.
 
Maneesh Godbole
Bartender
Posts: 11445
18
Android Eclipse IDE Google Web Toolkit Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The showXXXDialog takes an object as the message argument.
You can construct your tree and other required controls, put it on a panel, and pass the panel as the argument. Since panel is an Object, the dialog will happily display it correctly for you!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!