Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.24. Internal frames

5.24. Internal frames
Last updated: 25 January 2013.

This tutorial will show you how to use internal frames in Java.

Internal frames are instances of the class JInternalFrame. They are lightweight frames that are included in a JDesktopPane instance. Typically, to build a GUI with internal frames, you create JInternalFrame instances and add them to a JDesktopPane instance that is itself added to a JFrame instance. The following picture shows a GUI with 3 internal frames (the third one is iconified).


Menu bar with separators



Here is the code that builds the GUI shown above:

import javax.swing.JDesktopPane;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

public final class InternalFrames extends JFrame {

   
public InternalFrames() {
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents() {

         
// Create the desktop pane
         
JDesktopPane desktopPane = new JDesktopPane();

         
// Set up the first internal frame
         
JInternalFrame internalFrame1 = new JInternalFrame("Internal frame 1", true, true, true, true);
          internalFrame1.setSize
(300, 200);
          internalFrame1.setLocation
(20, 20);
          internalFrame1.setVisible
(true);
          desktopPane.add
(internalFrame1);

         
// Set up the second internal frame
         
JInternalFrame internalFrame2 = new JInternalFrame("Internal frame 2", true, true, true, true);
          internalFrame2.setSize
(300, 200);
          internalFrame2.setLocation
(350, 20);
          internalFrame2.setVisible
(true);
          desktopPane.add
(internalFrame2);

         
// Set up the third internal frame
         
JInternalFrame internalFrame3 = new JInternalFrame("Internal frame 3", true, true, true, true);
          internalFrame3.setSize
(300, 200);
          internalFrame3.setLocation
(20, 300);
          internalFrame3.setVisible
(true);
          desktopPane.add
(internalFrame3);

         
// Add the desktop pane to the JFrame
         
add(desktopPane);
   
}


   
private void init() {
         
try{
               
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
         
}
         
catch(Exception e){
               
e.printStackTrace();
         
}
         
setTitle("InternalFrames");
          setSize
(700, 400);
          setLocationRelativeTo
(null);
   
}

   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new InternalFrames();
               
}
          })
;
   
}
}

As you can see, the code is quite simple. I created internal frames that are resizable, closable, maximizable and iconifiable. The calls setSize(300, 200) and setVisible(true) ensure that each internal frame will be visible. If you omit the call setSize(300, 200), the internal frame cannot be visible since its size is 0. Also, like normal frames, you must invoke setVisible(true) on an internal frame to make it visible.

Naturally, you can use menus, layouts, and add components to an internal frame as you would do it for a normal JFrame instance. If you want to handle events fired by an internal frame, call the method addInternalFrameListener(InternalFrameListener l). The interface InternalFrameListener provides the following methods to handle events: internalFrameActivated, internalFrameClosed, internalFrameClosing, internalFrameActivated, internalFrameDeiconified, internalFrameIconified and internalFrameOpened.


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.24. Internal frames
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.25. Dialogs

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7