Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Event Queue kills GUI  RSS feed

Fintan Conway
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi All,

{Already tried this on java.sun forum - got no response. Hopefully you guys can help}
I have an application which was working fine. Basically a controller class loaded a Monitor class and a Screen class - both which extend JFrame. The class performed some background tasks and sent messages to the Monitor class and Screen class which updated their display to show the progress of the application. This was working fine BUT now I need to put a screen up which allows the users to change the parameters used by the program and kick off the process.

No problem I hear you say : Just add a JFrame with a main method that is called to initiate the application, call the Controller's constructor and call the method to run the process. This will show the screens and run the process as before.

Well NO. The process runs as before but the GUI does not invoke the Monitor class.

The following code shows the working application (without the Parameter view) :

Controller class pseudo code -

Monitor class

This works grand when the program is run by calling the main method of Controller. By stepping through the code I see that the code in the Main thread runs. Less than a second after the last line in the code labelled "// Thread = Main" is reached, the constructor for Monitor is activated in Thread = AWT-EventQueue-0, then the mon.showProgress() method is called, and when the run() method enclosed in the invokeLater() method completes the Monitor JFrame is displayed.
YIPEE works as planned.

However when I add the following class and run its main() method the whole GUI end blows up.

ParamView pseudo code :

The same as before happens except the runProc() method is called from the AWT-EventQueue-0 thread.

The run() method in the invokeLater() method is NOT called, therefore the constructor for the Monitor class is NOT called and the code continues processing. When the code reaches a point where it wants to print a message in the Monitor JFrame - the variable 'mon' has not been initialised and the application throws a NullPointerException. Interestingly enough this JFrame also shows exceptions when they occur and now the Monitor constructor is called.

Why does the AWT-EventQueue delay displaying this frame? How come it does not run when the 'mon' variable is called the first time, but does the second?

If I change the runProc() method to include the following :

The if() statement catch the AWT-EventQueue-0 thread and creates the Monitor outside the invokeLater code. Both the Monitor class and Screen class are invoked however they show as two grey boxes.

This is doing my head in, any help would be appreciated.

Many thanks,

Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!