![]() |
|||
![]() ![]() |
![]() |
|
![]() |
The MDITest application presented in Listing 17.5 creates an instance of the MDIFrame class and displays it using the Windows look-and-feel. The resulting frame, after adding a few internal frames, is shown in Figure 17.3. Notice that iconified internal frames can be seen at the bottom of the figure. This demonstrates that the internal frames are in fact internal. Iconified and maximized internal frames still reside within the enclosing JFrame instance. Listing 17.5 The MDITest Application package com.foley.test; import javax.swing.*; import com.foley.utility.MDIFrame; /** * An application that displays a frame that * contains internal frames in an MDI type * interface. * * @author Mike Foley **/ public class MDITest extends Object { /** * Application entry point. * Create the frame, and display it. * * @param args Command line parameter. Not used. **/ public static void main( String args[] ) { try { UIManager.setLookAndFeel( "com.sun.java.swing.plaf.windows.WindowsLookAndFeel" ); } catch( Exception ex ) { System.err.println( "Exception: " + ex.getLocalizedMessage() ); } JFrame frame = new MDIFrame( "MDI Test" ); frame.pack(); frame.setVisible( true ); } // main } // MDITest
By default, the contents of the internal frame are drawn as the frame is dragged. This feature makes dragging frames sluggish on many computers. To increase responsiveness while dragging frames, the frame may be dragged in outline mode when the default desktop manager is being used. Outline mode is enabled by setting the dragMode client property on the desktop pane to outline. Clearing this property, by setting its value to null, will enable the default drag mode. Unfortunately, both the property name and its one valid value are hard-coded strings in the DefaultDesktopManager class. Setting the dragMode property is demonstrated in the following code fragment. JDesktopPane desktopPane = new JDesktopPane();desktopPane.putClientProperty( "JDesktopPane.dragMode", outline" ); The MDIFrame class is by no means a complete MDI implementation. It does provide insight as to the JInternalFrame and JDesktopPane usage and how a complete MDI interface could be developed using these classes. Customizing the JInternalFrame InstancesAs was mentioned earlier in this chapter, the JInternalFrame class can be configured as the JFrame class is configured. This includes adding a toolbar and menu to the frame. These items take up valuable screen real estate and are not common on internal frames, but they are supported. The createInternalFrame method in the MDIFrame class can be changed to add these items. The updated method is shown next. It creates a JMenuBar instance and sets it on the internal frame by using the setJMenuBar method. A JToolBar is also created and added to the north region of the internal frames content pane. These are the same techniques used with the JFrame class. A new Action is created that disposes of the internal frame passed in its constructor. The action is added to both the menu and toolbar. The DisposeInternalFrameAction is also shown in Listing 17.6. The new version of the MDIFrame is shown in Figure 17.4. Listing 17.6 Modified Methods in the MDIFrame Class /** * Create an internal frame. * <p> * @return The newly created internal frame. **/ public JInternalFrame createInternalFrame() { final JInternalFrame internalFrame = new JInternalFrame( "Internal JLabel" ); JMenuBar menuBar = new JMenuBar(); JMenu file = new JMenu( "File" ); file.setMnemonic( KeyEvent.VK_F ); Action disposeAction = new DisposeInternalFrameAction( internalFrame ); Action exitAction = new ExitAction(); file.add( disposeAction ); file.add( exitAction ); menuBar.add( file ); internalFrame.setJMenuBar( menuBar ); JToolBar toolBar = new JToolBar(); toolBar.add( disposeAction ); toolBar.add( exitAction ); internalFrame.getContentPane().add( toolBar, BorderLayout.NORTH ); JComponent content = new JLabel( "Internal Frame Content" ); content.setBorder( BorderFactory.createLoweredBevelBorder() ); internalFrame.getContentPane().add( content, BorderLayout.CENTER ); internalFrame.setResizable( true ); internalFrame.setClosable( true ); internalFrame.setIconifiable( true ); internalFrame.setMaximizable( true ); internalFrame.pack(); return( internalFrame ); } /** * An Action that disposes of an internal frame. **/ public class DisposeInternalFrameAction extends AbstractAction { /** * The internal pane to close. **/ private JInternalFrame internalFrame; /** * CloseInternalFrameAction, constructor. * Set the name and icon for this action. * <p> * @param internalFrame The internal frame that we dispose of. **/ public DisposeInternalFrameAction( JInternalFrame internalFrame ) { super( "Close", new ImageIcon( "close.gif" ) ); this.internalFrame = internalFrame; } /** * Perform the action, dispose of the internal frame. * <p> * @param e The event causing us to be called. **/ public void actionPerformed( ActionEvent e ) { internalFrame.dispose(); } }
|
![]() |
|