Make sure Swing GUI components are created & updated on the EDT.
E.G. 1 (untested)
package com.evanmallory;
import java.awt.*;
import javax.swing.*;
public class TellTime extends JApplet {
private JLabel mMessage;
public TellTime() {
SwingUtilities.invokeLater( new Runnable() {
public void run() {
mMessage = new JLabel("Set the clock to the given time.",
SwingConstants.CENTER);
mMessage.setFont(new Font("Serif", Font.PLAIN, 36));
getContentPane().add(mMessage);
}
});
}
}
E.G. 2 (untested in anything but applet viewer)
Based on (ripped from) camickr's example, but with a call to setFont()
wrapped in a Timer
that fires every half second, and a subsequent call to repaint()
.
// <applet code="AppletBasic" width="300" height="100"></applet>
// The above line makes it easy to test the applet from the command line by using:
// appletviewer AppletBasic.java
import javax.swing.*;
import javax.swing.event.*;
import java.awt.*;
import java.awt.event.*;
import java.util.Random;
public class AppletBasic extends JApplet
{
Timer timer;
/**
* Create the GUI. For thread safety, this method should
* be invoked from the event-dispatching thread.
*/
private void createGUI()
{
final JLabel appletLabel = new JLabel( "I'm a Swing Applet" );
appletLabel.setHorizontalAlignment( JLabel.CENTER );
ActionListener listener = new ActionListener() {
Random random = new Random();
public void actionPerformed(ActionEvent ae) {
// determine a size between 12 & 36.
int size = random.nextInt(24)+12;
appletLabel.setFont(new Font("Serif", Font.PLAIN, size));
// tell the applet to repaint
repaint();
}
};
timer = new Timer(500, listener);
timer.start();
add( appletLabel );
}
public void init()
{
try
{
SwingUtilities.invokeAndWait(new Runnable()
{
public void run()
{
createGUI();
}
});
}
catch (Exception e)
{
System.err.println("createGUI didn't successfully complete: " + e);
}
}
}
And since I'm here.
<html>
<body>
<applet codebase="classes" code="com.evanmallory.TellTime.class"
width=800 height=500>
<param name="level" value="1"/>
<param name="topic" value="TellTime"/>
<param name="host" value="http://localhost:8080"/>
</applet>
</body>
</html>
- The
codebase="classes"
looks suspicious. If this were on a Java based server, the /classes
(and /lib
) directory would be for the exclusive use of the server, and would not be accessible to an applet.
- The code attribute should be the fully qualified name of the cass, so
com.evanmallory.TellTime.class
should be com.evanmallory.TellTime
.
<param name="host" value="http://localhost:8080"/>
I am prepared to make a WAG that that value is either unnecessary or wrong for time of deployment. It is better to determine at run-time using Applet.getDocumentBase()
or Applet.getCodeBase()
.
BTW - The applet worked fine in my recent FF running a recent Oracle Java. But EDT problems are non determinate (random), so that does not mean much.