'How can I call a GUI nested in EventQueue.invokeLater from the exterior?
How can I call a GUI element from the exterior?
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui = new NewJFrame().setVisible(true);
}
});
gui.textarea.something;
}
Solution 1:[1]
I also think this is an XY Problem. However, to answer the narrow question you have asked, just invoke invokeLater() a second time. I think perhaps the problem may just be the need to store the variable gui for later. The simple answer is that you just store it as normal, but NEVER access it from anywhere except the EDT.
public class MyApp {
// Never access except from the EDT
private static Object gui;
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui = new NewJFrame().setVisible(true);
}
});
// ... time passes ...
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui.textarea.something;
}
} );
}
}
Re. your comment:
If you're asking about modules, you are probably looking at some form of Dependency Injection. One way to do this is to make a special holder object to hold context information for your application. There's about a zillion ways to do this. Here's a simple one to get you started. A better method might be to use more, smaller context objects that refer to smaller portions of the application.
public static void main(String args[]) throws Exception {
GuiContext guic = new GuiContext();
java.awt.EventQueue.invokeAndWait(new Runnable() {
final GuiContext guiCtx = guic;
public void run() {
gui = new NewJFrame().setVisible(true);
guiCtx.setMainFrame( gui );
}
});
MyModule mod = new MyModule( guic );
mod.startMessage();
}
public class MyModule {
private GuiContext gui;
public MyModule( GuiContext gui ) { this.gui = gui; }
public void startMessage() {
gui.setMessage( "hello" );
}
}
public class GuiContext {
private Window gui;
public setGui( Window gui ) { this.gui = gui; }
public setMessage( final String message ) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
gui.add( message );
}
} );
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
