• Post Reply Bookmark Topic Watch Topic
  • New Topic

Launching a modal Dialog at app startup through invokeLater() or not?

 
Salman Ahmed
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am writing a Swing application that connects to a server-based system. At the startup of this application, it needs to prompt the user for a hostname/port pair so that it can connect to the specified server.

While I understand a little bit about the EDT and making GUI operations and manipulations through the invokeLater() helper method, I am a bit confused about how to launch my modal dialog at app. startup.

The call to myDialog.setVisible(true) will make the current thread pause until the user has accepted/dismissed the dialog but all this is done through EventQueue.invokeLater():

public static void main(String[] args) {
// TODO code application logic here
EventQueue.invokeLater(new Runnable() {
public void run() {
connectDialog = new ConnectDialog(null, true);
connectDialog.addWindowListener(new WindowAdapter() {
@Override
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
connectDialog.setVisible(true);
}
});

System.out.println("Got here!");
if ((connectDialog != null) &&
(connectDialog.getReturnStatus() == ConnectDialog.RET_CONNECT)) {
System.out.println("Connect button pressed");
}
}

How do I launch the dialog and obtain the return value (which of the buttons was pressed) in a thread-safe manner using this modal dialog? When I run my app as coded above, I am never able to detect if the Connect button on my dialog was pressed since the main thread goes motoring on after the call to invokeLater() and it seems that the dialog is contructed & launched after the main thread has finished executing.

I am pretty new to Java 1.5 and Swing, and am sure that I am missing something very basic here!

Thanks for any info!

Salman Ahmed
 
Marco Ehrentreich
best scout
Bartender
Posts: 1294
IntelliJ IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

look here under "Constructing a GUI in the Main Thread" for more information.

Marco
 
Salman Ahmed
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the link... I ended up using invokeAndWait() and it appears to do the right thing.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!