JInternal Frame in Java Swing

JInternalFrame is a class in Java Swing that provides a lightweight way to create internal frames within a main frame or a desktop pane.

Following are the steps to use JInternalFrame in Java Swing:

  1. Create a JDesktopPane object: A JDesktopPane is a container that holds internal frames. We can create it like this:
JDesktopPane desktopPane = new JDesktopPane();
  1. Create a JInternalFrame object: We can create an internal frame like this:
JInternalFrame internalFrame = new JInternalFrame("Internal Frame Title", true, true, true, true);

The parameters in the constructor are:

  • “Internal Frame Title”: The title of the internal frame
  • true: Whether the internal frame is resizable
  • true: Whether the internal frame is closable
  • true: Whether the internal frame is maximizable
  • true: Whether the internal frame is iconifiable
  1. Set the size and location of the internal frame: We can use the setSize() and setLocation() methods to set the size and location of the internal frame. Example:
internalFrame.setSize(300, 200);
internalFrame.setLocation(50, 50);
  1. Add components to the internal frame: We can add components such as buttons, text fields, etc. to the internal frame using the add() method. Example:
JButton button = new JButton("Click me");
internalFrame.add(button);
  1. Add the internal frame to the desktop pane: We can add the internal frame to the desktop pane using the add() method. Example:
desktopPane.add(internalFrame);
  1. Make the internal frame visible: We can make the internal frame visible by setting its visibility to true. For example:
internalFrame.setVisible(true);
  1. Add the desktop pane to the main frame: Finally, we can add the desktop pane to the main frame using the add() method. Example:
frame.add(desktopPane);

where frame is the main JFrame object.

That’s it! We can now create and display an internal frame in a desktop pane.

The complete example code is given below:

import javax.swing.*;
import java.awt.*;

public class InternalFrameExample {

    public static void main(String[] args) {
        
        // Create a JFrame
        JFrame frame = new JFrame("Internal Frame Example");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(600, 400);
        frame.setVisible(true);
        
        // Create a JDesktopPane
        JDesktopPane desktopPane = new JDesktopPane();
        
        // Create an internal frame
        JInternalFrame internalFrame = new JInternalFrame("Internal Frame Title", true, true, true, true);
        internalFrame.setSize(300, 200);
        internalFrame.setLocation(50, 50);
        
        // Add a button to the internal frame
        JButton button = new JButton("Click me");
        internalFrame.add(button);
        
        // Make the internal frame visible
        internalFrame.setVisible(true);
        
        // Add the internal frame to the desktop pane
        desktopPane.add(internalFrame);
        
        // Add the desktop pane to the main frame
        frame.add(desktopPane);
        
    }

}

When we run this code, it will create a main JFrame with an empty desktop pane. Then it will create an internal frame with a button, and add it to the desktop pane. Finally, it will display the internal frame within the main JFrame.

internal frame in java swing