JInternalFrame With JDesktop Pane in Swing

Introduction

 
In this article, we are going to describe how we use the Internal Frame with a Jdesktop pane in Java. And we also describe what an jinternal frame is and jdesktop and where they are used. JDesktop Pane is a container used to create a multiple-document interface or a virtual desktop. And this container is used to add jinternal frame objects. JInternal Frame is a class for displaying a frame like a window. Generally, you add internal frames to the desktop pane. The desktop pane, in turn, might be used as the content pane of a JFrame. The desktop pane is an instance of JDesktopPane, which is a subclass of JLayeredPane that has added API for managing multiple overlapping internal frames.
 
For easily creating an InternalFarme you have to use the following steps. 
 
Step 1 - Importing the necessary package
  1. import javax.swing.JInternalFrame;  
  2. import javax.swing.JDesktopPane;  
  3. import javax.swing.JMenu;  
  4. import javax.swing.JMenuItem;  
  5. import javax.swing.JMenuBar;  
  6. import javax.swing.JFrame;  
  7. import java.awt.event.*;  
  8. import java.awt.*;  
Step 2 - Creating a Class with constructor and generally most everything is defined within the constructor.
  1. public class TestJInternalFrame extends JFrame  
  2. {  
  3. JDesktopPane jdp;  
  4. static int fcount = 0;  
Step 3 - Constructor Defining 
  1. public TestJInternalFrame()  
  2. {  
  3.     //By using super keyword we pass my sting "JInternalFrame  Test" passing the to parent class constructor  
  4.     super("JInternalFrame Test");  
  5.     int inset = 50;  
  6.     //  here we try to getting your actual screen size by using toolKit class and its method getScreenSize()  
  7.     Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  8.     setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset * 2);  
  9.     // Add a Window Exit Listener and we create a anonymous class to perform window close operation  
  10.     addWindowListener(new WindowAdapter()  
  11.         {  
  12.             public void windowClosing(WindowEvent e)  
  13.             {  
  14.                 System.exit(0);  
  15.             }  
  16.         });  
  17.     // creating the object of Desktop pane because its need to adding internal frame  
  18.     jdp = new JDesktopPane();  
  19.     createFrame();  
  20.     setContentPane(jdp);  
  21.     setJMenuBar(createMenuBar());  
  22.     jdp.putClientProperty("JDesktopPane.with internal frame""outline");  
  23. }  
Step 4 - Now we create a menu bar and add a menu item Myframe:
  1. protected JMenuBar createMenuBar()  
  2. {  
  3.     JMenuBar menuBar = new JMenuBar();  
  4.     JMenu menu = new JMenu("MyFrame");  
  5.     menu.setMnemonic(KeyEvent.VK_N);  
  6.     JMenuItem menuItem = new JMenuItem("New IFrame");  
  7.     menuItem.setMnemonic(KeyEvent.VK_N);  
  8.     menuItem.addActionListener(new ActionListener()  
  9.         {  
  10.             public void actionPerformed(ActionEvent e)  
  11.             {  
  12.                 createFrame();  
  13.             }  
  14.         });  
  15.     menu.add(menuItem);  
  16.     menuBar.add(menu);  
  17.     return menuBar;  
  18. }  
Step 5 - Creating a frame. 
  1. protected void createFrame() {  
  2.     MyInternalFrame frame = new MyInternalFrame();  
  3.     frame.setVisible(true);  
  4.     jdp.add(frame);  
  5.     try  
  6.     {  
  7.         frame.setSelected(true);  
  8.     } catch (java.beans.PropertyVetoException e)  
  9.     {  
  10.         System.out.println(e);  
  11.     }  
  12. }  
Step 6 - Main method, and here we create the object of my class
  1. public static void main(String[] args) {  
  2.     TestJInternalFrame frame = new TestJInternalFrame();  
  3.     frame.setVisible(true);  
  4. }  
Step 7 - Now we are creating an internal frame class.
  1. class MyInternalFrame extends JInternalFrame  
  2. {  
  3.     static final int xCordinate = 40, yCordinate = 40;  
  4.     // constructor defination  
  5.     public MyInternalFrame()  
  6.     {  
  7.         super("Your Internal Frame " + (++fcount) + " is created"truetruetruetrue);  
  8.         setSize(300300);  
  9.         setVisible(true);  
  10.         setLocation(xCordinate * fcount, yCordinate * fcount);  
  11.     }  
  12. }  
Complete code
  1. import javax.swing.JInternalFrame;  
  2. import javax.swing.JDesktopPane;  
  3. import javax.swing.JMenu;  
  4. import javax.swing.JMenuItem;  
  5. import javax.swing.JMenuBar;  
  6. import javax.swing.JFrame;  
  7. import java.awt.event.*;  
  8. import java.awt.*;  
  9. public class TestJInternalFrame extends JFrame  
  10. {  
  11.     JDesktopPane jdp;  
  12.     static int fcount = 0;  
  13.     public TestJInternalFrame()  
  14.     {  
  15.         super("JInternalFrame Usage Demo");  
  16.         int inset = 50;  
  17.         Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();  
  18.         setBounds(inset, inset, screenSize.width - inset * 2, screenSize.height - inset * 2);  
  19.         addWindowListener(new WindowAdapter()  
  20.             {  
  21.                 public void windowClosing(WindowEvent e)  
  22.                 {  
  23.                     System.exit(0);  
  24.                 }  
  25.             });  
  26.         jdp = new JDesktopPane();  
  27.         createFrame();  
  28.         setContentPane(jdp);  
  29.         setJMenuBar(createMenuBar());  
  30.         jdp.putClientProperty("JDesktopPane.dragMode""outline");  
  31.     }  
  32.     protected JMenuBar createMenuBar()  
  33.     {  
  34.         JMenuBar menuBar = new JMenuBar();  
  35.         JMenu menu = new JMenu("Frame");  
  36.         menu.setMnemonic(KeyEvent.VK_N);  
  37.         JMenuItem menuItem = new JMenuItem("New IFrame");  
  38.         menuItem.setMnemonic(KeyEvent.VK_N);  
  39.         menuItem.addActionListener(new ActionListener()  
  40.             {  
  41.                 public void actionPerformed(ActionEvent e)  
  42.                 {  
  43.                     createFrame();  
  44.                 }  
  45.             });  
  46.         menu.add(menuItem);  
  47.         menuBar.add(menu);  
  48.         return menuBar;  
  49.     }  
  50.     protected void createFrame() {  
  51.         MyInternalFrame frame = new MyInternalFrame();  
  52.         frame.setVisible(true);  
  53.         jdp.add(frame);  
  54.         try  
  55.         {  
  56.             frame.setSelected(true);  
  57.         } catch (java.beans.PropertyVetoException e)  
  58.         {  
  59.             System.out.println(e);  
  60.         }  
  61.     }  
  62.     public static void main(String[] args) {  
  63.         TestJInternalFrame frame = new TestJInternalFrame();  
  64.         frame.setVisible(true);  
  65.     }  
  66.     class MyInternalFrame extends JInternalFrame  
  67.     {  
  68.         static final int xCordinate = 40, yCordinate = 40;  
  69.         public MyInternalFrame()  
  70.         {  
  71.             super("Your Internal Frame " + (++fcount) + " is created"truetruetruetrue);  
  72.             setSize(300300);  
  73.             setVisible(true);  
  74.             setLocation(xCordinate * fcount, yCordinate * fcount);  
  75.         }  
  76.     }  
  77. }  
Output
 
Sample Cmd output where you run your program.
 
jinternalcmd.jpg
 
This is the initial output.
 
jinternalframe.jpg 
 
Now you click on the frame option of the menu bar and then click new frame.
 
jinternalframe1.jpg 
 
Using the new frame option you can create n number of frames.
 
jinternalframe2.jpg 
 
Resources


Similar Articles