-1

There is SwingExplorer tool from this site http://www.swingexplorer.com/ that used to navigate swing content but how do I apply it to Applet?,especially if I want to integrate it to eclipse-plugin how do I configure the running configuration?

I guess that you need to supply the parameter of the applet that you want to run to AppletViwer and let the SwingExplorer navigate the AppletViewer(which in turn run your applet class) but i don't know how to pass such parameter to AppletViwer,Can anyone explain me how to do this?

Note that simply create new frame on top of applet and let it run as usual Swing application will not do because it need to be operated in browser-like environment.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user1560335
  • 79
  • 2
  • 11
  • *"it need to be operated in browser-like environment"* For what, specifically? ..JavaScript? *"thx for your answer"* Thanks for taking the time to help your communication by typing all 5 letters of this common phrase. ..Oh, you didn't. -1 for your laziness. – Andrew Thompson Oct 17 '13 at 05:09
  • For example,there is java.lang.NullPointerException throwed because method Applet.getDocumentBase() that was extended return null when I try to run it in swing application.It is legacy code ,the program is quite large and I don't want to change existing program to fix all of this problem.thx and hope for your understand. – user1560335 Oct 17 '13 at 06:07
  • *"thx and hope for your understand."* I understand you either did not get, or are ignoring, my message about spelling words properly. Since you could not be bothered doing so, my time is better spent elsewhere. – Andrew Thompson Oct 17 '13 at 06:11
  • OK,sorry for that,I don't know you talk about my spelling(I think you refer to how I explain my case,as you said I misreading it),thank you. – user1560335 Oct 17 '13 at 06:23
  • OK. Now we have cleared that up, see my answer. – Andrew Thompson Oct 17 '13 at 06:53
  • FYR: Applet patch for SwingExplorer 1.4 without eclipse integration https://gist.github.com/zakki/7020106 – zakki Oct 17 '13 at 07:10

1 Answers1

1

It is possible to provide a basic applet stub for an applet that is being hosted in a frame (a desktop app.). Several methods of the applet context are easy to reproduce in an application. The others are either harder, impractical to implement easily, or not relevant to a desktop based applet.

This example can be run as either an applet embedded in HTML or the applet viewer, or as an applet embedded in a desktop component (specifically a JOptionPane since the code is shorter).

The example was adapted from one in which the OP was more interested in the applet parameters. This version also adds support for reporting the document and code base.

/*
<applet code='DesktopEmbeddedApplet' width='400' height='100'>
<param name='param' value='embedded in applet viewer or the browser'>
</applet>
*/
import java.applet.*;
import java.awt.*;
import javax.swing.*;
import java.io.File;
import java.net.URL;
import java.util.HashMap;

public class DesktopEmbeddedApplet extends JApplet {

    public void init() {
        setLayout(new GridLayout(0,1));
        String param = getParameter("param");
        System.out.println("parameter: " + param);
        add(new JLabel(param));
        add(new JLabel("" + getDocumentBase()));
        add(new JLabel("" + getCodeBase()));
    }

    public static void main(String[] args) {
        ApplicationAppletStub stub = new ApplicationAppletStub();
        stub.addParameter("param", "embedded in application");
        DesktopEmbeddedApplet pa = new DesktopEmbeddedApplet();
        pa.setStub(stub);

        pa.init();
        pa.start();
        pa.setPreferredSize(new java.awt.Dimension(400,100));
        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() {
        URL url = null;
        try {
            url = new File(".").toURI().toURL();
        } catch(Exception e) {
            System.err.println("Error on URL formation!  null returned." );
            e.printStackTrace();
        }
        return url;
    }

    public URL getCodeBase() {
        URL url = null;
        try {
            url = new File(".").toURI().toURL();
        } catch(Exception e) {
            System.err.println("Error on URL formation!  null returned." );
            e.printStackTrace();
        }
        return url;
    }

    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);
    }
}
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433