Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Applet Question 1

Status
Not open for further replies.

kohinoor2007

Programmer
Mar 21, 2007
69
DE
Hi all,

Iam developing an applet in which ,I have overrided the init,start,stop & destroy methods.Iam using the j2sDK 1.4.

In the "init" method,I want to create and show the GUI.(That means dont want to load any data into the controls in the GUI).

Have a method createAndShowGui()--->which does that & is called from Applet "init".

In the "start" method,Iam loading the initial Datas that need to be loaded into the controls.

Have a method loadInitialData()--->which does that & is called from Applet "start".

Now,I have the following questions:

[1]Have read in the java tutorials that I should be using

javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run()
{
//
}

}
And inside the "run" method,I should be putting my calls to swingcomponents, as they are not mulltithreaded supported.

Now which of my above said 2 methods("createAndShowGui",which is called from "init" & "loadInitialData",which is called from "start"),should be put under "Run".

[2]Where should be the listeners added,in the method("createAndShowGui") called from "init" or from the method("loadInitialData") called from "start"

[3]"javax.swing.SwingUtilities.invokeLater"- What does it do?.Does it create a thread.
If yes,Do I have to explicitly remove it from the Applet "stop"or "destroy" methods.How ca I remove it then..


Thanks
 
javax.swing.SwingUtilities.invokeLater puts a Runnable entry into the Gui Event queue for running by the Gui Event Dispatcher thread when it gets around to it. This thread is responsible for driving the whole of the Swing/AWT Gui.

Tim
 
So tim,Do I have to call it explicitly in my case, which I have described in my post
 
Perhaps this will explain the issues

1) All the events and visual stuff in Swing/AWT is driven by the Event Dispatcher thread. When, for example, you catch an event such as ActionEvent, your code is being run by this thread. If your code then goes away and does some lengthy processing, you're stopping this thread from servicing the graphics system and all the visual repainting etc will cease until your process returns.

2) Conversely, if you have other threads on the go which handle the non-graphical processing, they must not be allowed to run code within the Swing classes. If you allow that, then you will have the proper Event Dispatch thread, and your own, both affecting the graphics and events and you will probably get some weird visual glitches as a result.

If you are sure there are no threads other than the Event Dispatcher, you don't need to worry much about invokeLater. However, if you create worker threads to avoid 1) then you'll have to guard against 2).

Tim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top