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