11 Replies Latest reply: May 31, 2012 2:10 PM by EJP RSS

    converting an applet to application

    939570
      Currently we have an applet we would like to build as an application as well. However, when we created the main() method all that is displayed when run as an application is the frame. As an applet it runs perfectly.

      The main() method is this:

      public static void main(String args[]) {
      JFrame mainWindow = new JFrame("Test");
      MyApplet app = new MyApplet();

      mainWindow.getContentPane().add(app);

      mainWindow.size(800, 600);
      mainWindow.show();

      app.init();
      app.start();
      }

      So the question is, how do we get the applet attached to the application so it can be run as both?
        • 1. Re: converting an applet to application
          morgalr
          In my experience, you do not, you write the app with the features of the applet.
          • 2. Re: converting an applet to application
            939570
            Okay, so the question still remains then is how do I make it work as both an applet and application? This method worked with the Java 1, so this feature has been removed? So I have to write it twice, once as an applet and once as an application?

            Will I simply have to rewrite the entire program in Java 1 and never use the newer features since we need that capability?
            • 3. Re: converting an applet to application
              EJP
              I'm not aware that method ever worked, and I have used every Java release since 1.1.2.
              • 4. Re: converting an applet to application
                gimbal2
                936567 wrote:
                Okay, so the question still remains then is how do I make it work as both an applet and application? This method worked with the Java 1, so this feature has been removed? So I have to write it twice, once as an applet and once as an application?
                Wouldn't it be more like write the app once, but make sure it can run as both an application (executable jar, run specific main() method which initializes app as a JFrame) and an applet?

                I'm asking out of general interest, I never bother with applets so I've never had to look into it before.
                • 5. Re: converting an applet to application
                  EJP
                  The basic approach is to write the application proper as a JComponent so you can add it to either JFrame or a JApplet.
                  • 6. Re: converting an applet to application
                    Darryl Burke
                    936567 wrote:
                    Currently we have an applet we would like to build as an application as well. However, when we created the main() method all that is displayed when run as an application is the frame. As an applet it runs perfectly.

                    The main() method is this:
                    public static void main(String args[]) {
                    JFrame mainWindow = new JFrame("Test");
                    MyApplet app = new MyApplet();
                    
                    mainWindow.getContentPane().add(app);
                    
                    mainWindow.size(800, 600);
                    mainWindow.show();
                    
                    app.init();
                    app.start();
                    }
                    So the question is, how do we get the applet attached to the application so it can be run as both?
                    1. Please read the announcements at the top of the forum listing and the FAQ linked from each page. They are there for a purpose.

                    2. Does MyApplet extend Applet or JApplet? If the latter then
                    mainWindow.setContentPane(app.getContentPane());
                    3. In any case, init() and start() the Applet before you set the JFrame visible.

                    4. Don't use the show() method which has been deprecated since Java 1.5, now already EOL. (edit: Also don't use size() which has been deprecated since 1.1)

                    db

                    Edited by: Darryl Burke
                    • 7. Re: converting an applet to application
                      Darryl Burke
                      EJP wrote:
                      I'm not aware that method ever worked, and I have used every Java release since 1.1.2.
                      Applet is a Component and isn't a top level Window (it extends Panel), so it can be add(...)ed to a Container.

                      db
                      • 8. Re: converting an applet to application
                        EJP
                        In which case it should still work.
                        • 9. Re: converting an applet to application
                          939570
                          I'll give that a try, but I find it interesting that you mention "size()" has been deprecated since 1.1 when according to the Java 1.4 documentation it's new replacing "setsize()"
                          • 10. Re: converting an applet to application
                            Darryl Burke
                            936567 wrote:
                            I find it interesting that you mention "size()" has been deprecated since 1.1 when according to the Java 1.4 documentation it's new replacing "setsize()"
                            Post a link or it isn't there.

                            db
                            • 11. Re: converting an applet to application
                              EJP
                              according to the Java 1.4 documentation it's new replacing "setsize()"
                              Please explain this fabrication. The 1.4 Javadoc for Component.size() says as follows:
                              Deprecated. As of JDK version 1.1, replaced by getSize().
                              Just as Darryl said.