Search code examples
javaswingjframeawtjapplet

Add JApplet to JFrame (or AWT Frame)


Is there a possibility to add an Applet (JBufferedApplet to be specific) to a JFrame (or an AWT Frame).

I've allready tried this, but it looks like the Applet simply doesn't run. It makes the background color of the JFrame gray (the same color of the Applet), but nothing more.

There is no possibility of changing the JApplet to a JPanel (I don't have access to the code).

All that has to be done for the moment is add the Applet to a JFrame/AWT Frame

This is the code I have so far:

import javax.swing.JFrame;

public class FormFrame extends JFrame {

    public FormFrame() {
        super("Oracle Forms");
        Main m = new Main();
        getContentPane().add(m); //add(m);
        setSize(800, 600);
        setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }

    public static void main(String[] args) {
        new FormFrame();
    }

}

All it gives is the background color of the Applet. It looks like the Applet doesn't run.


Solution

  • You could always try to add the applet's contentPane, something like:

    public class FormFrame extends JFrame {
    
       public FormFrame() {
           super("Oracle Forms");
           MyApplet myApplet = new MyApplet();
           myApplet.start();
           myApplet.init();
           getContentPane().add(myApplet.getContentPane()); 
           setSize(800, 600); // not sure about this.  Usually better to call pack();
           setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);
           setVisible(true);
       }
    
       public static void main(String[] args) {
          SwingUtilities.invokeLater(new Runnable() {
             public void run() {
                new FormFrame();
             }
          });
       }
    }
    

    Just don't forget to call the applet's init() method to allow it to initialize all its components.

    Edit: changes made for thread safety as per trashgod's excellent recommendation.