Paste third-party JApplet into Swing GUI and pass its parameters

There is a third-party applet that I would like to integrate into my Swing application. Basically, I would like it to be another panel. This applet uses many parameters, for example.

final String config_filename = getParameter(XXX); 

I saw a lot of documentation on how to send parameter values ​​via HTML, but how do you do this with code (or possibly property files)? Any help would be appreciated!

+6
source share
2 answers

AppletStub and set it as the stub of the applet instance. For instance.

 /* <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> 
+13
source

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

The Applet.getParameter() method simply delegates the applet stub, so in your special case it may be enough just to implement the AppletStub (the methods it needs) and pass this by composing the AppletContext. You can also leave without calling some or even all of the life cycle methods.

+6
source

Source: https://habr.com/ru/post/889037/


All Articles