Deitel & Associates, Inc. Logo

Back to www.deitel.com
digg.png delicious.png blinkit.png furl.png
Java How to Program, 6/e

ISBN:
0-13-148398-6
© 2005
pages: 1576

Buy the Book!
Amazon logo
InformIT logo

Many of today's desktop applications use a multiple-document interface (MDI)-a main window containing other windows-to manage several open documents. This tutorial demonstrates Java's JDesktopPane and JInternalFrame classes for implementing multiple-document interfaces. The tutorial is intended for students and professionals who are familiar with basic Java GUI and event handling techniques.

Download the code examples for this tutorial.

[Note: This tutorial is an excerpt (Section 22.7) of Chapter 22, GUI Components: Part 2, from our textbook Java How to Program, 6/e. This tutorial may refer to other chapters or sections of the book that are not included here. Permission Information: Deitel, Harvey M. and Paul J., JAVA HOW TO PROGRAM, ©2005, pp.1026-1030. Electronically reproduced by permission of Pearson Education, Inc., Upper Saddle River, New Jersey.]

22.7 Multiple Document Interfaces with JDesktopPane and JInternalFrame

Many of today's applications use a multiple-document interface (MDI)a main window (called the parent window) containing other windows (called child windows), to manage several open documents that are being processed in parallel. For example, many e-mail programs allow you to have several windows open at the same time, thso you can compose or read multiple e-mail messages simultaneously. Similarly, many word processors allow the user to open multiple documents in separate windows, making it possible to switch between them without having to close one to open another. The application in Fig. 22.11 and Fig. 22.12 demonstrates Swing's JDesktopPane and JInternalFrame classes for implementing multiple-document interfaces.

Fig. 22.11 | Multiple-document interface.
   1     // Fig. 22.11: DesktopFrame.java
   2     // Demonstrating JDesktopPane.
   3     import java.awt.BorderLayout;
   4     import java.awt.Dimension;
   5     import java.awt.Graphics;
   6     import java.awt.event.ActionListener;
   7     import java.awt.event.ActionEvent;
   8     import java.util.Random;
   9     import javax.swing.JFrame;
   10   import javax.swing.JDesktopPane;
   11   import javax.swing.JMenuBar;
   12   import javax.swing.JMenu;
   13   import javax.swing.JMenuItem;
   14   import javax.swing.JInternalFrame;
   15   import javax.swing.JPanel;
   16   import javax.swing.ImageIcon;
   17   
   18   public class DesktopFrame extends JFrame
   19   {
   20      private JDesktopPane theDesktop;
   21   
   22      // set up GUI
   23      public DesktopFrame()
   24      {
   25         super( "Using a JDesktopPane" );
   26   
   27         JMenuBar bar = new JMenuBar(); // create menu bar
   28         JMenu addMenu = new JMenu( "Add" ); // create Add menu
   29         JMenuItem newFrame = new JMenuItem( "Internal Frame" );
   30   
   31         addMenu.add( newFrame ); // add new frame item to Add menu
   32         bar.add( addMenu ); // add Add menu to menu bar
   33         setJMenuBar( bar ); // set menu bar for this application
   34   
   35         theDesktop = new JDesktopPane(); // create desktop pane
   36         add( theDesktop ); // add desktop pane to frame
   37   
   38          // set up listener for newFrame menu item
   39         newFrame.addActionListener(
   40   
   41            new ActionListener() // anonymous inner class
   42            {
   43               // display new internal window
   44               public void actionPerformed( ActionEvent event )
   45               {
   46              // create internal frame
   47                     JInternalFrame frame = new JInternalFrame(
   48                     "Internal Frame", true, true, true, true );
   49   
   50                     MyJPanel panel = new MyJPanel(); // create new panel
   51                     frame.add( panel, BorderLayout.CENTER ); // add panel
   52                     frame.pack(); // set internal frame to size of contents
   53   
   54                     theDesktop.add( frame ); // attach internal frame
   55                     frame.setVisible( true ); // show internal frame
   56                  } // end method actionPerformed
   57               } // end anonymous inner class
   58            ); // end call to addActionListener
   59         } // end DesktopFrame constructor
   60   } // end class DesktopFrame
   61   
   62   // class to display an ImageIcon on a panel
   63   class MyJPanel extends JPanel
   64   {
   65      private static Random generator = new Random();
   66      private ImageIcon picture; // image to be displayed
   67      private String[] images = { "yellowflowers.png", "purpleflowers.png",
   68         "redflowers.png", "redflowers2.png", "lavenderflowers.png" };
   69   
   70         // load image
   71         public MyJPanel()
   72         {
   73            int randomNumber = generator.nextInt( 5 );
   74            picture = new ImageIcon( images[ randomNumber ] ); // set icon
   75         } // end MyJPanel constructor
   76   
   77         // display imageIcon on panel
   78         public void paintComponent( Graphics g )
   79         {
   80            super.paintComponent( g );
   81            picture.paintIcon( this, g, 0, 0 ); // display icon
   82         } // end method paintComponent
   83   
   84         // return image dimensions
   85         public Dimension getPreferredSize()
   86         {
   87            return new Dimension( picture.getIconWidth(),
   88               picture.getIconHeight() );
   89         } // end method getPreferredSize
   90   } // end class MyJPanel

Lines 27–33 create a JMenuBar, a JMenu and a JMenuItem, add the JMenuItem to the JMenu, add the JMenu to the JMenuBar and set the JMenuBar for the application window. When the user selects the JMenuItem newFrame, the application creates and displays a new JInternalFrame object containing an image.

Line 35 assigns JDesktopPane (package javax.swing) variable theDesktop a new JDesktopPane object that will be used to manage the JInternalFrame child windows. Line 36 adds the JDesktopPane to the JFrame. By default, the JDesktopPane is added to the center of the content pane's BorderLayout, so the JDesktopPane expands to fill the entire application window.

Lines 39–58 register an ActionListener to handle the event when the user selects the newFrame menu item. When the event occurs, method actionPerformed (lines 44–56) creates a JInternalFrame object in lines 47–48. The JInternalFrame constructor used here takes five arguments—a string for the title bar of the internal window, a boolean indicating whether the internal frame can be resized by the user, a boolean indicating whether the internal frame can be closed by the user, a boolean indicating whether the internal frame can be maximized by the user and a boolean indicating whether the internal frame can be minimized by the user. For each of the boolean arguments, a true value indicates that the operation should be allowed (as is the case here).

Fig. 22.12 | Test class for DeskTopFrame.
   1   // Fig. 22.12: DesktopTest.java
   2   // Demonstrating JDesktopPane.
   3   import javax.swing.JFrame;
   4   
   5   public class DesktopTest
   6   {
   7      public static void main( String args[] )
   8      {
   9           DesktopFrame desktopFrame = new DesktopFrame();
   10         desktopFrame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
   11         desktopFrame.setSize( 600, 480 ); // set frame size
   12         desktopFrame.setVisible( true ); // display frame
   13      } // end main
   14   } // end class DesktopTest

As with JFrames and JApplets, a JInternalFrame has a content pane to which GUI components can be attached. Line 50 creates an instance of our class MyJPanel (declared at lines 63–90) that is added to the JInternalFrame at line 51.

Line 52 uses JInternalFrame method pack to set the size of the child window. Method pack uses the preferred sizes of the components to determine the window's size. Class MyJPanel declares method getPreferredSize (lines 85–89) to specify the panel's preferred size for use by the pack method. Line 54 adds the JInternalFrame to the JDesktopPane, and line 55 displays the JInternalFrame.

Classes JInternalFrame and JDesktopPane provide many methods for managing child windows. See the JInternalFrame and JDesktopPane online API documentation for complete lists of these methods:

java.sun.com/j2se/5.0/docs/api/javax/swing/JInternalFrame.html
    java.sun.com/j2se/5.0/docs/api/javax/swing/JDesktopPane.html