How to convert a Swing Application to an Applet?

fromvega picture fromvega · Mar 16, 2009 · Viewed 14.6k times · Source

I created a desktop application with Swing Application Framework, now how can I convert it to an applet? The main class extends SingleFrameApplication.

EDITED: This is the starting class, used NetBeans GUI builder:

public class PhotoApp extends SingleFrameApplication {

    /**
     * At startup create and show the main frame of the application.
     */
    @Override protected void startup() {
        show(new PhotoView(this));
    }

    /**
     * This method is to initialize the specified window by injecting resources.
     * Windows shown in our application come fully initialized from the GUI
     * builder, so this additional configuration is not needed.
     */
    @Override protected void configureWindow(java.awt.Window root) {
    }

    /**
     * A convenient static getter for the application instance.
     * @return the instance of PhotoUploaderApp
     */
    public static PhotoApp getApplication() {
        return Application.getInstance(PhotoApp.class);
    }

    /**
     * Main method launching the application.
     */
    public static void main(String[] args) {
        launch(PhotoApp.class, args);
    }
}

Answer

Chris Cudmore picture Chris Cudmore · Mar 16, 2009

The quick and dirty way:

Drop extends SingleFrameApplication. Add extends JApplet.

replace the constructor with public void init() leaving the body as is.

Create an HTML page to hold it. and give it a whirl.

There will likely be some scoping issues, but you should be able to fix those fairly easily.