Reputation: 16319
There's a nice discussion on EventQueue.invokeLater() here.
I have a controller class, Master()
that loads two UI windows in my application. For example:
public class Master(){
public Master(){
aView = new subView();
bView = new subView();
Where subView
extends JFrame and has the following main method:
public class SubView extends JFrame{
....
public static void main(String args[]) {
java.awt.EventQueue.invokeLater(new Runnable() {
public void run() {
new SubView();
}
});}
}
Notice that SubView.main()
uses the invokeLater()
. My question is how can I invokeLater() within master? Something like:
public class Master(){
public Master(){
aView = EventQueue.invokeLater(new subView);
bView = EventQueue.invokeLater(new subView);
It's not this simple because invokeLater does not return anything. Furthermore, because it's "invoked later", the values of aView and bView remain null in Master. Is there anyway to invoke both in the same manner that main()
would invoke one of them in the runLater thread?
Upvotes: 0
Views: 376
Reputation: 205875
It may prove awkward to solve this problem by invoking instances of Runnable
. As an alternative use a SwingWorker
to update the table models of both the master and detail views. This example may be a useful staring point.
Upvotes: 1
Reputation: 16319
Following Peter's suggestion, I did the following:
I created an intermediate Runnable called RunUIThread
that returns that exposes the objects aView
and bView
, so I can return them to my Master program. Peter, do you think this is valid?
RunUIThread uiThread = new RunUIThread();
try {
java.awt.EventQueue.invokeAndWait(uiThread);
} catch (InterruptedException | InvocationTargetException ex) {
LOG.error("MasterView interrupted or failed to invoke RunUIThread");
}
aView = uiThread.getaView();
bView = uiThread.getbView();
Upvotes: 0
Reputation: 533880
I would use invokeAndWait as you need to wait for the outcome.
SubView aView, bView;
public Master() {
java.awt.EventQueue.invokeAndWait(new Runnable() {
public void run() {
aView = new SubView();
bView = new SubView();
}
});
// aView and bView will be initialised.
}
Upvotes: 1