Now you have two streams: main stream and EDT (event sending stream). I suppose you know that SwingUtilities.invokeLater(runnable) runs the task on EDT.
To exchange data between threads, you just need a variable that is included in the volume of both threads. The easiest way to achieve this is to declare a volatile or AtomicReference data element in a class containing the main method.
To make sure you read the value after it returns, JOptionPane , the easiest thing you can do here is to change the invokeLater call to invokeAndWait . This will cause the main thread to stop executing until you finish what you put on the EDT.
Example:
public class MyClass { private static volatile String mySharedData; public static void main(String[] args) throws InterruptedException { SwingUtilities.invokeAndWait(new Runnable() { public void run() { mySharedData = JOptionPane.showInputDialog(null, "Stop ?", JOptionPane.QUESTION_MESSAGE); } });
If your main thread performs some task that should not stop while the options panel is present, then you can periodically check in the main thread (that is, in the outer part of the loop in which your task is executed), regardless of whether or not mySharedData . If your task does not loop and instead performs some I / O or wait operations, you can use Thread.interrupt and check mySharedData in InterruptedExecption handlers.
source share