Open In App

Java Swing | Internal Frame with examples

Last Updated : 14 Apr, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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 
 

  1. JInternalFrame() : creates a new non- closable, non- resizable, non- iconifiable, non- maximizable JInternalFrame with no title
  2. JInternalFrame(String t) :creates a new non- closable, non- resizable, non- iconifiable, non- maximizable JInternalFrame with a title specified
  3. JInternalFrame(String t, boolean resizable) :creates a new non- closable, non- iconifiable, non- maximizable JInternalFrame with a title and resizability specified
  4. JInternalFrame(String t, boolean resizable, boolean closable) : creates a new non- iconifiable, non- maximizable JInternalFrame with a title, closability and resizability specified
  5. JInternalFrame(String t, boolean resizable, boolean closable, boolean maximizable) :creates a new non- iconifiable JInternalFrame with a title, closability, maximizability and resizability specified 
     
  6. 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 
 

  1. setFrameIcon(Icon icon) : sets the icon for the frame to the specified image
  2. setLayout(LayoutManager manager) : sets the layout of the frame to specified layout manager
  3. setTitle(String t): set the title of the frame to specified title
  4. getTitle() : get the title of the frame
  5. reshape(int x, int y, int width, int height) : resize the frame to specified width and height and a specified location
  6. add(Component c) : adds the specified component to the container.
  7. addImpl(Component c, Object co, int i) : adds the specified component.
  8. addInternalFrameListener(InternalFrameListener l) : adds the specified InternalFrameListener to the list.
  9. createRootPane() : called by the constructor to set up the JRootPane.
  10. dispose() : makes this internal frame invisible, unselected, and closed.
  11. fireInternalFrameEvent(int id) : fires an internal frame event.
  12. getAccessibleContext() : gets the AccessibleContext associated with this JInternalFrame.
  13. getContentPane() : returns the content pane for this internal frame.
  14. getDefaultCloseOperation() : returns the default operation that occurs when the user initiates a “close” on this internal frame.
  15. getDesktopIcon() : returns the JDesktopIcon used when this JInternalFrame is iconified.
  16. getDesktopPane() : convenience method that searches the ancestor hierarchy for a JDesktop instance.
  17. getFocusOwner() : If this JInternalFrame is active, returns the child that has focus.
  18. getFrameIcon() : returns the image displayed in the title bar of this internal frame
  19. getGlassPane() : returns the glass pane for this internal frame.
  20. getInternalFrameListeners() : Returns an array of all the InternalFrameListeners added to this JInternalFrame with addInternalFrameListener
  21. getJMenuBar() : returns the current JMenuBar for this JInternalFrame
  22. getLastCursor() : returns the last Cursor that was set by the setCursor method
  23. getLayer() : convenience method for getting the layer attribute of this component.
  24. getLayeredPane() : returns the layered pane for this internal frame.
  25. getMostRecentFocusOwner() : returns the child component of this JInternalFrame that will receive the focus when this JInternalFrame is selected.
  26. getNormalBounds() : If the JInternalFrame is not in maximized state, returns getBounds(); otherwise, returns the bounds that the JInternalFrame would be restored to.
  27. getRootPane(): returns the rootPane object for this internal frame.
  28. getUI() : returns the look-and-feel object that renders this component.
  29. getWarningString() : gets the warning string that is displayed with this internal frame.
  30. isClosable() : returns whether this JInternalFrame can be closed by some user action.
  31. isClosed() : Returns whether this JInternalFrame is currently closed.
  32. isIcon() : returns whether the JInternalFrame is currently iconified.
  33. isMaximizable() : gets the value of the maximizable property.
  34. isMaximum() : returns whether the JInternalFrame is currently maximized.
  35. isResizable() : returns whether the JInternalFrame can be resized or not.
  36. isSelected() : returns whether the JInternalFrame is the currently active frame or not.
  37. pack() : causes components of this JInternalFrame to be laid out at their preferred size.
  38. paintComponent(Graphics g) : Overridden to allow optimized painting when the internal frame is being dragged.
  39. paramString() : Returns a string representation of this JInternalFrame.
  40. remove(Component c) : removes the specified component from the container.
  41. removeInternalFrameListener(InternalFrameListener l) : removes the specified internal frame listener.
  42. setClosable(boolean b) : sets whether this JInternalFrame can be closed by some user action.
  43. setContentPane(Container c) : sets this JInternalFrame’s contentPane property.
  44. setCursor(Cursor c) : sets the cursor image to the specified cursor.
  45. setDefaultCloseOperation(int o): sets the operation that will happen by default when the user initiates a “close” on this internal frame.
  46. setDesktopIcon(JInternalFrame.JDesktopIcon d) : sets the JDesktopIcon associated with this JInternalFrame.
  47. setGlassPane(Component g) : sets this JInternalFrame’s glassPane property.
  48. setIcon(boolean b) : Iconifies or de-iconifies this internal frame.
  49. setJMenuBar(JMenuBar m) : sets the menuBar property for this JInternalFrame.
  50. setIconifiable(boolean b) : sets the iconable property, which must be true for the user to be able to make the JInternalFrame an icon.
  51. setJMenuBar(JMenuBar m) : sets the menuBar property for this JInternalFrame.
  52. setLayer(int l) : convenience method for setting the layer attribute of this component.
  53. setLayer(Integer l) : convenience method for setting the layer attribute of this component.
  54. setLayeredPane(JLayeredPane l) : sets this JInternalFrame’s layeredPane property.
  55. setMaximizable(boolean b) : sets the maximizable property, which determines whether the JInternalFrame can be maximized by some user action.
  56. setMaximum(boolean b) : Maximizes and restores this internal frame.
  57. setNormalBounds(Rectangle r) : sets the normal bounds for this internal frame.
  58. setResizable(boolean b) :sets whether the JInternalFrame can be resized by some user action.
  59. setRootPane(JRootPane r) : sets the rootPane property for this JInternalFrame.
  60. setRootPaneCheckingEnabled(boolean e) : sets whether calls to add and setLayout are forwarded to the contentPane.
  61. setSelected(boolean s) : selects or deselects the internal frame if it’s showing.
  62. setUI(InternalFrameUI ui) : sets the UI delegate for this JInternalFrame.
  63. show() : makes the internal frame visible.
  64. toBack(): sends this internal frame to the back.
  65. toFront() : Brings this internal frame to the front.
  66. 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
 



Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads