Java Swing | Internal Frame with examples
Last Updated :
14 Apr, 2021
JInternalFrame is a part of Java Swing . JInternalFrame is a container that provides many features of a frame which includes displaying title, opening, closing, resizing, support for menu bar, etc.
Constructors for JInternalFrame
- JInternalFrame() : creates a new non- closable, non- resizable, non- iconifiable, non- maximizable JInternalFrame with no title
- JInternalFrame(String t) :creates a new non- closable, non- resizable, non- iconifiable, non- maximizable JInternalFrame with a title specified
- JInternalFrame(String t, boolean resizable) :creates a new non- closable, non- iconifiable, non- maximizable JInternalFrame with a title and resizability specified
- JInternalFrame(String t, boolean resizable, boolean closable) : creates a new non- iconifiable, non- maximizable JInternalFrame with a title, closability and resizability specified
- JInternalFrame(String t, boolean resizable, boolean closable, boolean maximizable) :creates a new non- iconifiable JInternalFrame with a title, closability, maximizability and resizability specified
- JInternalFrame(String t, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable) : creates a new JInternalFrame with a title, closability, maximizability, iconifiability and resizability specified
Commonly used methods
- setFrameIcon(Icon icon) : sets the icon for the frame to the specified image
- setLayout(LayoutManager manager) : sets the layout of the frame to specified layout manager
- setTitle(String t): set the title of the frame to specified title
- getTitle() : get the title of the frame
- reshape(int x, int y, int width, int height) : resize the frame to specified width and height and a specified location
- add(Component c) : adds the specified component to the container.
- addImpl(Component c, Object co, int i) : adds the specified component.
- addInternalFrameListener(InternalFrameListener l) : adds the specified InternalFrameListener to the list.
- createRootPane() : called by the constructor to set up the JRootPane.
- dispose() : makes this internal frame invisible, unselected, and closed.
- fireInternalFrameEvent(int id) : fires an internal frame event.
- getAccessibleContext() : gets the AccessibleContext associated with this JInternalFrame.
- getContentPane() : returns the content pane for this internal frame.
- getDefaultCloseOperation() : returns the default operation that occurs when the user initiates a "close" on this internal frame.
- getDesktopIcon() : returns the JDesktopIcon used when this JInternalFrame is iconified.
- getDesktopPane() : convenience method that searches the ancestor hierarchy for a JDesktop instance.
- getFocusOwner() : If this JInternalFrame is active, returns the child that has focus.
- getFrameIcon() : returns the image displayed in the title bar of this internal frame
- getGlassPane() : returns the glass pane for this internal frame.
- getInternalFrameListeners() : Returns an array of all the InternalFrameListeners added to this JInternalFrame with addInternalFrameListener
- getJMenuBar() : returns the current JMenuBar for this JInternalFrame
- getLastCursor() : returns the last Cursor that was set by the setCursor method
- getLayer() : convenience method for getting the layer attribute of this component.
- getLayeredPane() : returns the layered pane for this internal frame.
- getMostRecentFocusOwner() : returns the child component of this JInternalFrame that will receive the focus when this JInternalFrame is selected.
- getNormalBounds() : If the JInternalFrame is not in maximized state, returns getBounds(); otherwise, returns the bounds that the JInternalFrame would be restored to.
- getRootPane(): returns the rootPane object for this internal frame.
- getUI() : returns the look-and-feel object that renders this component.
- getWarningString() : gets the warning string that is displayed with this internal frame.
- isClosable() : returns whether this JInternalFrame can be closed by some user action.
- isClosed() : Returns whether this JInternalFrame is currently closed.
- isIcon() : returns whether the JInternalFrame is currently iconified.
- isMaximizable() : gets the value of the maximizable property.
- isMaximum() : returns whether the JInternalFrame is currently maximized.
- isResizable() : returns whether the JInternalFrame can be resized or not.
- isSelected() : returns whether the JInternalFrame is the currently active frame or not.
- pack() : causes components of this JInternalFrame to be laid out at their preferred size.
- paintComponent(Graphics g) : Overridden to allow optimized painting when the internal frame is being dragged.
- paramString() : Returns a string representation of this JInternalFrame.
- remove(Component c) : removes the specified component from the container.
- removeInternalFrameListener(InternalFrameListener l) : removes the specified internal frame listener.
- setClosable(boolean b) : sets whether this JInternalFrame can be closed by some user action.
- setContentPane(Container c) : sets this JInternalFrame's contentPane property.
- setCursor(Cursor c) : sets the cursor image to the specified cursor.
- setDefaultCloseOperation(int o): sets the operation that will happen by default when the user initiates a "close" on this internal frame.
- setDesktopIcon(JInternalFrame.JDesktopIcon d) : sets the JDesktopIcon associated with this JInternalFrame.
- setGlassPane(Component g) : sets this JInternalFrame's glassPane property.
- setIcon(boolean b) : Iconifies or de-iconifies this internal frame.
- setJMenuBar(JMenuBar m) : sets the menuBar property for this JInternalFrame.
- setIconifiable(boolean b) : sets the iconable property, which must be true for the user to be able to make the JInternalFrame an icon.
- setJMenuBar(JMenuBar m) : sets the menuBar property for this JInternalFrame.
- setLayer(int l) : convenience method for setting the layer attribute of this component.
- setLayer(Integer l) : convenience method for setting the layer attribute of this component.
- setLayeredPane(JLayeredPane l) : sets this JInternalFrame's layeredPane property.
- setMaximizable(boolean b) : sets the maximizable property, which determines whether the JInternalFrame can be maximized by some user action.
- setMaximum(boolean b) : Maximizes and restores this internal frame.
- setNormalBounds(Rectangle r) : sets the normal bounds for this internal frame.
- setResizable(boolean b) :sets whether the JInternalFrame can be resized by some user action.
- setRootPane(JRootPane r) : sets the rootPane property for this JInternalFrame.
- setRootPaneCheckingEnabled(boolean e) : sets whether calls to add and setLayout are forwarded to the contentPane.
- setSelected(boolean s) : selects or deselects the internal frame if it's showing.
- setUI(InternalFrameUI ui) : sets the UI delegate for this JInternalFrame.
- show() : makes the internal frame visible.
- toBack(): sends this internal frame to the back.
- toFront() : Brings this internal frame to the front.
- updateUI() : notification from the UIManager that the look and feel has changed.
1. Program to create a simple JInternalFrame :
Java
// java Program to create a simple JInternalFrame
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {
// frame
static JFrame f;
// label to display text
static JLabel l;
// main class
public static void main(String[] args)
{
// create a new frame to
f = new JFrame("frame");
// create a internal frame
JInternalFrame in = new JInternalFrame();
// set the title of the frame
in.setTitle("InternalFrame");
// create a Button
JButton b = new JButton("button");
// create a label to display text
l = new JLabel("This is a JInternal Frame ");
// create a panel
JPanel p = new JPanel();
// add label and button to panel
p.add(l);
p.add(b);
// set visibility internal frame
in.setVisible(true);
// add panel to internal frame
in.add(p);
// add internal frame to frame
f.add(in);
// set the size of frame
f.setSize(300, 300);
f.show();
}
}
Output :
2. program to create multiple internal frames
Java
// java Program to create multiple internal frames
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {
// frame
static JFrame f;
// label to display text
static JLabel l, l1;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// set layout of frame
f.setLayout(new FlowLayout());
// create a internal frame
JInternalFrame in = new JInternalFrame("frame 1", true, true, true, true);
// create a internal frame
JInternalFrame in1 = new JInternalFrame("frame 2", true, true, true, true);
// create a Button
JButton b = new JButton("button");
JButton b1 = new JButton("button1");
// create a label to display text
l = new JLabel("This is a JInternal Frame no 1 ");
l1 = new JLabel("This is a JInternal Frame no 2 ");
// create a panel
JPanel p = new JPanel();
JPanel p1 = new JPanel();
// add label and button to panel
p.add(l);
p.add(b);
p1.add(l1);
p1.add(b1);
// set visibility internal frame
in.setVisible(true);
in1.setVisible(true);
// add panel to internal frame
in.add(p);
in1.add(p1);
// add internal frame to frame
f.add(in);
f.add(in1);
// set the size of frame
f.setSize(300, 300);
f.show();
}
}
Output :
3 . Program to create multiple frame and set icon to the frame
Java
// java Program to create multiple frame and set icon to the frame
import java.awt.event.*;
import java.awt.*;
import javax.swing.*;
class solution extends JFrame {
// frame
static JFrame f;
// label to display text
static JLabel l, l1;
// main class
public static void main(String[] args)
{
// create a new frame
f = new JFrame("frame");
// set layout of frame
f.setLayout(new FlowLayout());
// create a internal frame
JInternalFrame in = new JInternalFrame("frame 1",
true, true, true, true);
// create a internal frame
JInternalFrame in1 = new JInternalFrame("frame 2",
true, true, true, true);
// set icon for internal frames
in.setFrameIcon(new ImageIcon("f:/gfg.jpg"));
in1.setFrameIcon(new ImageIcon("f:/gfg.jpg"));
// create a Button
JButton b = new JButton("button");
JButton b1 = new JButton("button1");
// create a label to display text
l = new JLabel("This is a JInternal Frame no 1 ");
l1 = new JLabel("This is a JInternal Frame no 2 ");
// create a panel
JPanel p = new JPanel();
JPanel p1 = new JPanel();
// add label and button to panel
p.add(l);
p.add(b);
p1.add(l1);
p1.add(b1);
// set visibility internal frame
in.setVisible(true);
in1.setVisible(true);
// add panel to internal frame
in.add(p);
in1.add(p1);
// add internal frame to frame
f.add(in);
f.add(in1);
// set the size of frame
f.setSize(300, 300);
f.show();
}
}
Output :
Note : the above program might not run in an online compiler please use an offline IDE
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read