views:

4347

answers:

11

Hi, 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!

A: 

"The import javax.swing cannot be resolved" - sorry, I'm not a GWT maven, but this error is classpath-esque. Sounds like GWT can't find the rt.jar for your JVM.

duffymo
+1  A: 

Google found this. One of the responses says: "The previous poster is right, the shell can not handle embedded things like Flash or Applets. There are some restrictions in the SWT component used to run the browser inside of the shell. A bug report has been associated with this issue, you may want to keep an eye on it for future updates."

Looks like it can't be done.

duffymo
A: 

I saw that same thing, but I thought it only applied to Hosted mode. I was hoping I could compile the GWT code into the resulting javascript/html and run it in a browser. But you're right - somehow it's not finding the right jar. I edited the "foo-compile.cmd" script that GWT uses to include all those jars, though:

@java -Xmx256M -cp "%~dp0\src;%~dp0\bin;C:/gwt-windows-1.5.3/gwt-user.jar;C:/gwt-windows-1.5.3/gwt-dev-windows.jar;c:/gwtext-2.0.5/gwtext.jar;C:/Program Files/Java/jdk1.6.0_10/jre/lib/plugin.jar;C:/Program Files/Java/jdk1.6.0_10/jre/lib/javaws.jar;C:/Program Files/Java/jdk1.6.0_10/jre/lib/deploy.jar;C:/Program Files/Java/jdk1.6.0_10/jre/lib/resources.jar;C:/Program Files/Java/jdk1.6.0_10/jre/lib/rt.jar" com.google.gwt.dev.GWTCompiler -out "%~dp0\www" %* com.andesa.ItmClient

A: 

It's a legacy app that is too big to rewrite in one go. We want to keep the existing functionality while we convert it over to GWT code piecemeal.

A: 

I'm wondering if there's a way to cheat it if it's not possible within GWT. Maybe create a () frame that can be handled with straight html?

A: 

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?

+3  A: 

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&amp;s=google-web-toolkit-doc-1-5&amp;t=RefJreEmulation

Yoni Roit
+2  A: 

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.

Andrew Newdigate
A: 

Thanks! uzhin and Andrew! That makes sense. I'll try moving my applet outside the GWT module.

A: 

Take a look at http://code.google.com/p/gwtai/.

fklomp
A: 

A light-heavyweight app might mix GWT and JNLP. Then we could get the bigger jars onto people's machines rather transparently. As an example, I'd like to use the Batik toolkit or other SVG-related goodies to have SVG in my GWT app, rather than being forced to use only png or other raster formats.

Bob Futrelle