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 (http://download.oracle.com/javase/tutorial/deployment/applet/getStarted.html) 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.

1
2
3
4
5
6
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(HelloWorld.java:10)

The problem is that we already execute the Applet.init() method in the event thread, and calling invokeAndWait in the event thread would cause a deadlock.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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");
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }
}

Arguably, this applet is implemented in a strange way. invokeAndWait 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.

Share

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