I recommend creating a class for processing, samples below:
class SyncUserData implements Runnable { private String value ; public void run() { value = JOptionPane.showInputDialog("Enter host name: ") ; } public String getValue() { return value ; } }
I will continue this approach by making an abstraction of the class and using generics so that any value types are returned.
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 from EventDispatchThread works.
if (SwingUtilities.isEventDispatchThread()) { host = doHostnameGen.process() ; } else { SwingUtilities.invokeAndWait(doHostnameGen) ; host = doHostnameGen.getValue() ; }
source share