Dan Marcus wrote:
<SNIP>
Hi Dan,
I changed and tested this and it works.
//------------- <FIXED_HTML> ------------------------
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML>
<HEAD>
<TITLE>This should show a frame</TITLE>
</HEAD>
<BODY>
<APPLET width="200" height="100" code="AppletWithFrame" ></APPLET>
</BODY>
</HTML>
//----------- </FIXED_HTML>
My methods are unconventional insofar as
every Java book/example I have ever seen, but
they are derived over many years and solve many
issues of many Java versions.
That is they were not construed in a vacuum...
(And they generally work) Hmm touch wood :)
There are several rules I ues.
#1 ALWAYS imlement the Runnable interface
This leads to the next statement.
#2 ALWAYS overwirte start() stop() l(ike in this example)
This works around many issues pertianing to Threads
across different JVMS and versions.
# ALWAYS synchronized on start() stop() and init()
this FORCES serialization of the whole initialization
process and avoids several pitfalls such as your frame
being access before it is in fact initialized.
The "boolean initialized" is there to work
around an issue of the older browsers calling
start()/stop()/init() multiple times in rapid succession.
It also allows you to retain the Applet context if the
user leaves the page and comes back under
pre 1.3.1 JVMs. The lastest versions destroy
everything when you leave the page.
//---------------- Working APPLET
import java.awt.*;
public class AppletWithFrame extends java.applet.Applet implements Runnable{
Thread task = null;
boolean initialized = false; // This works around
Frame frame = null; // multiple initialization issues.
int iteration = 0;
public synchronized void start()
{
task = new Thread(this);
task.start();
}
public synchronized void stop()
{
task = null;
}
public synchronized void init () {
if(initialized)
return;
add(new Label("Here is a label to show it's loaded."));
frame = new Frame("Frame's up");
frame.add(new Label("Here's a frame label"));
frame.pack();
frame.show();
initialized = true;
}
public synchronized void run()
{
Thread controlThread = Thread.currentThread();
while(controlThread == task)
{
try{
wait(1000);
}
catch(InterruptedException ie)
{
}
getAppletContext().showStatus("Running "+iteration++);
}
}
// Mr. Kok's solution to the window freeze bug
// under the old 1.1.5 JVM
// Basically wase some time to allow
// for post processing.
// Does not apply to newer JVMs.
public synchronized void destroy()
{
try
{
wait(1000);
}
catch(InterruptedException ie)
{
}
}
}
//------------------------------
This should work for you.
Goood Luck!
Sincerely:
--
Tony Swain
Senior V.P. of Software Development Hyperbyte inc.
http://www.hyperbyte.ab.ca
Netscape DevEdge Champion Devs-Java Newsgroup
snews://secnews.netscape.com/netscape.devs-java