4

I have a big Swing project. Is there any tool that convert the Swing application jar file into an applet?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
confucius
  • 13,127
  • 10
  • 47
  • 66

3 Answers3

4

If the original project creates a JFrame, you could simply get the JFrame's contentPane via

Container contentPane = myFrame.getContentPane();

And then add it the JApplet's as its contentPane, but you would lose menus and such.

For e.g.,

public class MyApplet extends JApplet {

   @Override
   public void init() {
      try {
         SwingUtilities.invokeAndWait(new Runnable() {
            public void run() {
               JFrame myFrame = new MyFrame("Frame");
               Container contentPane = myFrame.getContentPane();
               setContentPane(contentPane);
            }
         });
      } catch (InterruptedException e) {
         e.printStackTrace();
      } catch (InvocationTargetException e) {
         e.printStackTrace();
      }
   }
}

Better to have most of your GUI classes geared to create JPanels from the get go so you don't run into this limitation.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
Hovercraft Full Of Eels
  • 283,665
  • 25
  • 256
  • 373
  • 3
    Note that most applications do things that are not allowed in a sand-boxed applet, such as calling `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)` or `System.exit(0)`. It is never as simple as it seems to launch a frame from an applet, or use its content. – Andrew Thompson Mar 20 '12 at 16:49
  • +1 for well-factored content, which admits a _hybrid_ initialization mentioned [here](http://stackoverflow.com/a/9106263/230513). – trashgod Mar 20 '12 at 17:27
3

As an alternative, you might look to launch the current app. from a link using Java Web Start. No conversion necessary, and much easier than deploying an applet.

Community
  • 1
  • 1
Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
2

The tool is 4 lines of code long:

public class MyApplet extends Applet {
    public void start() {
         MyApplication.main(new String[0]);
    }
}

This applet will run your application without command line arguments. The limitation here is that the appet will open its own window. It will not run into browser window. It is not trivial task to run it into browser in common case because the layout may be dynamic and may be created on the JFrame itself. So, you need some knowledge of the aplication structure to fully port it. The good news that in most cases it is not so hard. Just call code that adds components to JFrame with applet itself that extends Panel and therefore is container.

If your application receives parameters you should implement init() method of applet.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • 1
    *"This applet will run your application without command line arguments."* Every time `start()` is called (e.g. each time the browser is restored from being minimized). Note that most applications do things that are not allowed in a sand-boxed applet, such as calling `frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE)` or `System.exit(0)`. It is never as simple as it seems to launch a frame from an applet, or use its content. – Andrew Thompson Mar 20 '12 at 16:48