Problem in DrJava Applet Viewer

I just read the following thumbs-down review for DrJava on the SourceForge website:

There is a severe defect. The HelloWorld Java applet ( crashes in the Applet Viewer invoked by DrJava. Still it runs smootly both in IE8 and FireFox. It also runs OK in the Applet Viewer started separately, without DrJava.

There really is a problem. The HelloWorld applet throws an exception.

[cc]Welcome to DrJava. Working directory is D:\Documents\Dev\Java
> run HelloWorld
Exception in thread “AWT-EventQueue-0″ java.lang.Error: Cannot call invokeAndWait from the event dispatcher thread
at java.awt.EventQueue.invokeAndWait(Unknown Source)
at javax.swing.SwingUtilities.invokeAndWait(Unknown Source)
at HelloWorld.init([/cc]

The problem is that we already execute the [cc_java inline=”true”]Applet.init()[/cc_java] method in the event thread, and calling [cc_java inline=”true”]invokeAndWait[/cc_java] in the event thread would cause a deadlock.

[cc_java]import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import javax.swing.JLabel;

public class HelloWorld extends JApplet {
//Called when this applet is loaded into the browser.
public void init() {
//Execute a job on the event-dispatching thread; creating this applet’s GUI.
try {
SwingUtilities.invokeAndWait(new Runnable() {
public void run() {
JLabel lbl = new JLabel(“Hello World”);
} catch (Exception e) {
System.err.println(“createGUI didn’t complete successfully”);

Arguably, this applet is implemented in a strange way. [cc_java inline=”true”]invokeAndWait[/cc_java] should run the code immediately if it is already running in the event thread. I’ll see what I can do to fix this.

I would have preferred it if the reviewer had just filed a bug report and given us the opportunity to fix this first.


About Mathias

Software development engineer. Principal developer of DrJava. Recent Ph.D. graduate from the Department of Computer Science at Rice University.
This entry was posted in DrJava, Ramblings. Bookmark the permalink.

Leave a Reply