Roland Sams
Roland Sams

Reputation: 173

How to create a JFrame within a Java applet or vice versa

I am trying to create a Java applet with JInternalFrames. For this I believe you need a JFrame in one form or another. I heard you could wrap your applet in a JFrame, is that best? If you could tell me what I should do I would appreciate it. TYIA -Roland

/**
 * @(#)rebuiltgui.java
 *
 * rebuiltgui Applet application
 *
 * @author 
 * @version 1.00 2013/1/21
 */

import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.plaf.basic.BasicInternalFrameUI;
import java.awt.event.*;
import java.awt.Desktop;
import javax.swing.*;
import javax.swing.BoxLayout;
import java.awt.*;
import java.applet.*;

public class rebuiltgui extends JApplet {

    public void init() {

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

            public void run() {

                createAndShowGUI();
            }
        });
    }

    private void createAndShowGUI() {

   // This is where I would put the internal frames.
    }
}

Upvotes: 0

Views: 13073

Answers (1)

Audrius Meškauskas
Audrius Meškauskas

Reputation: 21778

You can create JFrame in the applet code same way you do in a stand alone Swing application:

JFrame frame = new JFrame("Hello");
....
frame.show();

The frame will leave the browser window, showing up as a separate window, just it will contain some warning tags that this is a Java applet running. Such frame can be resized or moved around.

Actually, some "applets" do not show much more than a single button to launch the frame in they main view.

See http://baba.sourceforge.net/ for an example of the applet that shows four buttons to launch four different visualizations (source code is available).

If, very differently, you actually want frames that could not leave your applet area, use InternalFrame. It is a LayeredPane so you can also put your main component as a background layer.

Upvotes: 3

Related Questions