aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Applet question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Applet question" Watch "Applet question" New topic
Author

Applet question

Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
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
Craig Wood
Ranch Hand

Joined: Jan 14, 2004
Posts: 1535
[1]Have read in the java tutorials that I should be using "SwingUtilities.invokeLater"
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".

The init method is called and executed before the applet is realized/made_visible. So for
anything called in or from init you do not have to be concerned with the event dispatch
thread.
The start method is called by the AppletContext at the first showing and again every time
the applet is restored after being minimized. You may need to make some special
arrangements if you do not want to re–initialize your gui every time this happens. It is
okay to load data when this (start) method is called. If the data–loading is complex or
takes a long time and you want your gui to remain responsive to user interaction/use then
it is wise to do the work on a background thread, not on the event dispatch thread. We use
invokeLater to update the gui after realization. This is not always required. Some things
like calling repaint and setting/changing text on a JLabel or text component can be done
without using invokeLater. So you have to experiment to find out what you need in your
applet. You can try loading the data in a background thread and if it does not update the
gui properly (from the background thread) then you can use invokeLater (in the background
thread) to update the gui as needed.


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

This is a good question. If updating the gui components will/does not trigger events that
you cannot deal with then you can add them in the createAndShowGui method. If there is
trouble with undesired events being triggered then you will need to make some special
arrangements to avoid the side–effects. If needed you could delay adding listeners until
after the initialization in loadInitialData. Or even in a call to invokeLater (as above).
You just have to experiment to find what it takes to get what you want. Also, what might
happen if the user starts interacting with your gui components in a way that generates
events while the gui is waiting for the initialization to complete?

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

No, it is designed to be care–free. You do not have to be concerned with removing it.
Arun Sanker
Ranch Hand

Joined: Mar 21, 2007
Posts: 44
Thank You...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Applet question