6

There's a third-party applet that I'd like to embed in my Swing application. Basically, I'd like it to be just another panel. This applet makes use of many parameters, e.g.

final String config_filename = getParameter(XXX);

I've seen lots of documentation about how to send parameters values via HTML, but how do you do it via code (or perhaps property files)? Any help would be appreciated!

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
kc2001
  • 5,008
  • 4
  • 51
  • 92

2 Answers2

14

Implement an AppletStub & set it as the stub of the applet instance. E.G.

/*
<applet code='ParamApplet' width='200' height='200'>
<param name='param' value='foo'>
</applet>
*/
import java.applet.*;
import javax.swing.*;
import java.net.URL;
import java.util.HashMap;

public class ParamApplet extends JApplet {

    public void init() {
        String param = getParameter("param");
        System.out.println("parameter: " + param);
        add(new JLabel(param));
    }

    public static void main(String[] args) {
        ApplicationAppletStub stub = new ApplicationAppletStub();
        stub.addParameter(args[0], args[1]);
        ParamApplet pa = new ParamApplet();
        pa.setStub(stub);

        pa.init();
        pa.start();
        pa.setPreferredSize(new java.awt.Dimension(200,200));
        JOptionPane.showMessageDialog(null, pa);
    }
}

class ApplicationAppletStub implements AppletStub {

    HashMap<String,String> params = new HashMap<String,String>();

    public void appletResize(int width, int height) {}
    public AppletContext getAppletContext() {
        return null;
    }

    public URL getDocumentBase() {
        return null;
    }

    public URL getCodeBase() {
        return null;
    }

    public boolean isActive() {
        return true;
    }

    public String getParameter(String name) {
        return params.get(name);
    }

    public void addParameter(String name, String value) {
        params.put(name, value);
    }
}

Typical I/O

prompt>java ParamApplet param "apples & oranges"
parameter: apples & oranges

prompt>java ParamApplet param 42
parameter: 42

prompt>
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • Works great, thanks! Both your answer and Paŭlo's were good, but I'm choosing yours, because it's almost foolproof. Cheers to both of you! – kc2001 May 26 '11 at 19:29
7

For a full applet environment, you have to implement AppletContext and AppletStub (see Andrew's answer for a minimal example), and then pass the last one to setStub of your applet after creating the instance with the constructor. You also should take care of calling the applet's lifecycle methods init(), start(), stop() and destroy() (after setting the applet stub).

The Applet.getParameter() method simply delegates to the applet stub, thus in your special case it might be enough only implementing AppletStub (the needed methods of it) and pass this, ommiting the AppletContext. You might also get away without calling some or even all of the lifecycle methods.

Community
  • 1
  • 1
Paŭlo Ebermann
  • 73,284
  • 20
  • 146
  • 210