How To Create Internal Frames In Java

In this tutorial we will learn about how to create a frame within a frame.

How To Create Internal Frames In Java

In this tutorial we will learn about how to create a frame within a frame.

How To Create Internal Frames In Java

How To Create Internal Frames In Java

In this tutorial we will learn about how to create a frame within a frame.

Some times you will need to display a frame till the application is running and want to do operations on an another frames lying within the internal frames. To create internal frames in Java you may use javax.swing.JInternalFrame class. This class has various constructors using which you can create frames within a frame with different features like maximizable/not maximizable internal frame, resizable/not resizable internal frame, closable/not closable internal frame.

Constructors Of JInternalFrame

Constructor Detail Description
JInternalFrame() This constructor creates an internal frame that haven't a title, can't be maximize, resize, close and non-iconifiable.
JInternalFrame(String title) This constructor creates an internal frame with the title, but can't be maximize, resize, close and non-iconifiable.
JInternalFrame(String title, boolean resizable) This constructor creates an internal frame with title that can be resize but, can't be maximize, close and non-iconifiable.
JInternalFrame(String title, boolean resizable, boolean closable) This constructor creates an internal frame with title that can be resize, close but, can't be maximize and non-iconifiable.
JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable) This constructor creates an internal frame with the tile that can be resize, maximize, close, but non-iconifiable.
JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) This constructor creates an internal frame with the title, that can be resize, maximize, close, and iconifiable.

Following important points should be remember when creating internal frames

  • Size of the internal frame must be set. Default size of internal frame is zero, so it will not display anything. To set the size of internal frame following methods can be used : setSize(), pack(), setBounds().
  • Internal frame must be added to container. If it is not added to the container it will be not appear. Generally, the internal frame is added to the JDesktopPane.
  • Like, the JFrame to show or make visible the internal frame following method must be invoked setVisible(true) or show().

Example

As we discussed above JInternalFrame class provides constructors for creating frames within a Frame. Here we will give a simple example which will demonstrate you how to create frames within a frame. In this example we will create different internal frames with different features. In this example we will create a frame using JFrame that contains a button. When you will click the button then the internal frames will be created on the click event. In this example we will define two methods from one of which named createMainFrameGUI we will create an instance of JFrame and some components such as button. In the second method named createInternalFrameGUI we will create multiple instance of JInternalFrame into  which each, represents a different internal frames.

CreateInternalFrame.java

import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JDesktopPane;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import java.awt.*;
import java.awt.event.*;

public class CreateInternalFrame {
	
	JFrame frame;
	JInternalFrame inner1, inner2, inner3, inner4, inner5, inner6;
	JButton button;
	JPanel panel;
	JLabel lable;
	JDesktopPane desktop = new JDesktopPane();	
	boolean resizable = true, closable = true, maximizable = true,
	iconifiable = true;
	int pos = 25;
	int width = 200, height = 100; 
	
	public void createMainFrameGUI()
	{
		frame = new JFrame("Main Frame");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);		
		frame.setLayout(new BorderLayout());		
		button = new JButton("Create Frames");
		panel = new JPanel();
		button.setBounds(50, 150, 100, 20);
		button.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent ae)
			{
				createInternalFrameGUI();
			}
		}
		);
		panel.add(button);		
		frame.add(panel);		
		frame.setVisible(true);
		frame.setSize(400, 300);		
	}
	
	public void createInternalFrameGUI()
	{
		inner1 = new JInternalFrame();
		inner1.setBounds(0, 0, width, height);
		
		inner2 = new JInternalFrame("Frame title1");
		inner2.setBounds(1*pos, 1* pos, width, height);
		
		inner3 = new JInternalFrame("Frame title2", resizable);
		inner3.setBounds(2 * pos, 2 * pos, width, height);
		
		inner4 = new JInternalFrame("Frame title3", resizable, closable);
		inner4.setBounds(3 * pos, 3 * pos, width, height);
		
		inner5 = new JInternalFrame("Frame title4", resizable, closable, maximizable);
		inner5.setBounds(4 * pos, 4 * pos, width, height);
		
		inner6 = new JInternalFrame("Frame title5", resizable, closable, maximizable, iconifiable);
		inner6.setBounds(5 * pos, 5 * pos, width, height);
		
		inner1.setVisible(true);
		inner2.setVisible(true);
		inner3.setVisible(true);
		inner4.setVisible(true);
		inner5.setVisible(true);
		inner6.setVisible(true);
		
		desktop.add(inner6);
		desktop.add(inner5);
		desktop.add(inner4);
		desktop.add(inner3);
		desktop.add(inner2);
		desktop.add(inner1);
		frame.add(desktop);
		frame.setContentPane(desktop);
		desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE);
	}
	public static void main(String args[])
	{
		CreateInternalFrame cif = new CreateInternalFrame();
		cif.createMainFrameGUI();
	}
}

Output :

When you will compile and execute the above Java class CreateInternalFrame.java then the output will be as follows :

When you will click on the button "Create Frames" then the output will be as follows :

Download Source Code