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.

Figure 22.11. Multiple-document interface.

(This item is displayed on pages 1027 - 1028 in the print version)

 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

Figure 22.12. Test class for DeskTopFrame.

(This item is displayed on pages 1029 - 1030 in the print version)

 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
 

Lines 2733 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 3958 register an ActionListener to handle the event when the user selects the newFrame menu item. When the event occurs, method actionPerformed (lines 4456) creates a JInternalFrame object in lines 4748. The JInternalFrame constructor used here takes five argumentsa 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).

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 6390) 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 8589) 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

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover



Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615

Flylib.com © 2008-2020.
If you may any questions please contact us: flylib@qtcs.net