Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to display java applet inside GWT page?

I'm probably missing something simple here, but I can't find the answer elsewhere. I just want to display an applet in my GWT code.

OS: Windows XP Java: JDK 1.6.0_10 Other: GWT, GWT-Ext 2.0.5

Here is the applet (obviously simplified for testing):

package foo.applet;

import javax.swing.JApplet;
import java.awt.Graphics;

public class HelloApplet extends JApplet 
{
    public void paint(Graphics g) 
    {
        g.drawRect(0, 0, 
                   getSize().width - 1,
                   getSize().height - 1);
        g.drawString("Hello world!", 5, 15);
    }
}

Here is the code calling it:


package foo.applet;

import com.google.gwt.user.client.ui.HTML;
import com.gwtext.client.widgets.Panel;


public class AppletPanel extends Panel 
{
public AppletPanel()
{
    HTML applet = new HTML();
    applet.setHTML("<applet name=\"HelloApplet\" code=\"HelloApplet.class\" width=\"300\" height=\"300\"" );
    this.add(applet);
}

}

When I launch the app in hosted mode, the jvm crashes (filed incident 1425130 with Sun).

When I try to compile the GWT code for running in a browser, I get this:

        [ERROR] Errors in 'file:/C:/<blah>/applet/HelloApplet.java'
           [ERROR] Line 3: The import javax.swing cannot be resolved
           [ERROR] Line 4: The import java.awt cannot be resolved
           [ERROR] Line 6: JApplet cannot be resolved to a type
           [ERROR] Line 8: Graphics cannot be resolved to a type
           [ERROR] Line 11: The method getSize() is undefined for the type HelloApplet
           [ERROR] Line 12: The method getSize() is undefined for the type HelloApplet

Obviously I'm missing some applet library, but I've grepped through all the jars in the jdk and tried including all of the ones that list JApplet or awt (plugin.jar, resources.jar, rt.jar, deploy.jar, javaws.jar).

Also, I'm pretty sure once I solve this problem there's another one lurking right after it, but I'll save that for another question.

Thanks!


The legacy app is not an applet - it is a thick-client Swing application. We've hacked it to run as an applet because our customers want a browser client and this is the fastest way to get that done.

I don't know if GWT would accept the JPanel solution - the app is not written in any way that GWT can parse - i.e. it's not using the GWT API, it's using the Swing API. AFAIK, the only way to mix Swing with GWT would be in an applet fashion.

Am I missing something?

like image 625
user9945 Avatar asked Dec 22 '22 13:12

user9945


2 Answers

Are you trying to GWT-compile your applet?

This won't work, as GWT compilation (which is just translation from Java to Javascript) supports only handful of Java libraries and certainly not applets.

Make sure that your applet is not on GWT source path (move it to another package).

Reference: http://code.google.com/docreader/#p=google-web-toolkit-doc-1-5&s=google-web-toolkit-doc-1-5&t=RefJreEmulation

like image 93
Yoni Roit Avatar answered Jan 02 '23 14:01

Yoni Roit


Don't use the GWTCompiler to compile your applet code. I would recommend creating a second module (or project) that contains only the applet code. Compile this to a separate JAR using the standard Javac compiler (or your IDE/ant)

The GWTCompiler uses a subset of the Java libraries, and should only be used to generate the code which is going to eventually run as Javascript.

like image 36
Andrew Newdigate Avatar answered Jan 02 '23 12:01

Andrew Newdigate