|
|
The InternalFrameTest application is shown in Listing 17.1. In the main method, an ApplicationFrame is created and displayed. A JInternalFrame instance is created and configured to be resizable, closable, iconifiable, and maximizable. There are also variants of the JInternalFrame class constructor that allow these properties to be specified at construction time. The internal frame is then added to the center of the content pane of the application frame. The resulting windows for the Java and Windows look-and-feel are shown in Figure 17.1. The internal frame class Java look-and-feel is very different than the Windows look-and-feel. The icons and resize borders are very unique. The Java look-and-feel also contains the controversial notch under the controls on the right side of the titlebar. Unfortunately, the icons and border cannot be set to match those found on the top-level frame. This is because the Windows window manager defines the look of those controls, and they cannot be altered by the application. This fact makes the Java look-and-feel less appealing for internal frames.
Listing 17.1 The InternalFrameTest Application
package com.foley.test;
import java.awt.*;
import javax.swing.*;
import com.foley.utility.ApplicationFrame;
/**
* An application that displays a frame that
* contains internal frames.
*
* @author Mike Foley
**/
public class InternalFrameTest extends Object {
/**
* Application entry point.
* Create the frame, and display it.
*
* @param args Command line parameter. Not used.
**/
public static void main( String args[] ) {
//
// Un-comment these lines to enable the Windows LAF.
//
// try {
// UIManager.setLookAndFeel(
// "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" );
// } catch( Exception ex ) {
// System.err.println( "Exception: " +
// ex.getLocalizedMessage() );
// }
JFrame frame = new ApplicationFrame( "Internal Frame Test" );
JInternalFrame internalFrame =
new JInternalFrame( "Internal JLabel" );
internalFrame.getContentPane().add( new JLabel( "Hello" ) );
internalFrame.setResizable( true );
internalFrame.setClosable( true );
internalFrame.setIconifiable( true );
internalFrame.setMaximizable( true );
internalFrame.pack();
frame.getContentPane().add( internalFrame, BorderLayout.CENTER );
frame.pack();
frame.setVisible( true );
} // main
} // InternalFrameTest
Figure 17.1 Internal frames in the Java (top) and Windows (bottom) look-and-feels.
The InternalFrameTest application shows that JInternalFrame instances can be used like any other component. Although the class is used primarily in an MDI type mode, it can be used with any layout manager. However, current bugs in the JInternalFrame class cause NullPointerException exceptions to be thrown when the mouse is clicked over the internal frame.
The methods to enable an internal frame to be iconifiable, maximizable, and closable were presented earlier. If these properties are enabled, there are also methods that allow the application to set and query the corresponding states. For example, if the internal frame is closable, the isClosed method can be used to determine if it is currently closed, and the setClosed method can be used to close the internal frame. The dispose method is called when the internal frame is no longer required. The methods for an iconifiable internal frame are isIcon, to determine if the frame is currently iconified, and setIcon, to iconify and restore the internal frame. For the maximizable property, the associated methods are isMaximum and setMaximum.
|