I'll recommend to create a class to handle this, samples below:
class SyncUserData implements Runnable {
private String value ;
public void run() {
value = JOptionPane.showInputDialog("Enter host name: ") ;
}
public String getValue() {
return value ;
}
}
// Using an instance of the class launch the popup and get the data.
String host;
SyncUserData syncData = new SyncUserData() ;
SwingUtilities.invokeAndWait(syncData);
host = syncData.getValue() ;
I'll extend this approach by making the class abstract and using generics to allow any type of values to be return.
abstract class SyncUserDataGeneric<Type> implements Runnable {
private Type value ;
public void run() {
value = process();
}
public Type getValue() {
return value ;
}
public abstract Type process() ;
}
String host;
SyncUserDataGeneric<String> doHostnameGen ;
doHostnameGen = new SyncUserDataGeneric<String>() {
public String process() {
return JOptionPane.showInputDialog("Enter host name: ");
}
};
host = doHostnameGen.getValue() ;
EDIT: Checks if running from the EventDispatchThread.
if (SwingUtilities.isEventDispatchThread()) {
host = doHostnameGen.process() ;
} else {
SwingUtilities.invokeAndWait(doHostnameGen) ;
host = doHostnameGen.getValue() ;
}