Brought to you by EarthWeb
ITKnowledge Logo Login Graphic Click NOW to join Planet IT!
Click NOW to join Planet IT!
ITKnowledge
Search this book:
 
Search the site:
 
EXPERT SEARCH ----- nav

EarthWeb Direct

EarthWeb Direct

EarthWeb sites: other sites

Previous Table of Contents Next


Integrating JInternalFrame and JDesktopPane Usage

For the JInternalFrame class to shine, it must be used in conjunction with the JDesktopPane class. When an internal frame is added to a desktop pane, it behaves as an independent frame in the desktop pane. It can be dragged, iconified, maximized, and sized independently of other internal frames contained in the desktop pane.

The MDIFrame class shown in Listing 17.4 is an example frame showing how a desktop pane can be added to a JFrame instance. The MDIFrame class is a JFrame extension that configures itself with a JDesktopPane instance in the center region of its content pane. An Action is added to the File menu and to the toolbar that creates and adds an internal frame to the desktop pane when invoked. The internal frame creation method adds a JLabel instance to the content pane of the internal frame. This is for demonstration only. Your application would add its content to the internal frame instance.

Listing 17.4 The MDIFrame Class

package com.foley.utility;

import java.awt.*;

import java.awt.event.*;

import java.io.Serializable;

import javax.swing.*;

import javax.swing.border.*;

import javax.swing.event.*;

/**
 * A top-level frame. The frame configures itself
 * with a JDesktopPane in its content pane.
 *
 * @author Mike Foley
 **/
public class MDIFrame extends JFrame implements Serializable {

    /**
     * The desktop pane in our content pane.
     **/
    private JDesktopPane desktopPane;

    /**
     * MDIFrame, null constructor.
     **/
    public MDIFrame() {
        this( null );
    }

    /**
     * MDIFrame, constructor.
     *
     * @param title The title for the frame.
     **/
    public MDIFrame( String title ) {
        super( title );
    }

    /**
     * Customize the frame for our application.
     **/
    protected void frameInit() {
        //
        // Let the super create the panes.
        super.frameInit();

        JMenuBar menuBar = createMenu();
        setJMenuBar( menuBar );

        JToolBar toolBar = createToolBar();
        Container content = getContentPane();
        content.add( toolBar, BorderLayout.NORTH );

        desktopPane = new JDesktopPane();
        desktopPane.setPreferredSize( new Dimension( 400, 300 ) );
        content.add( desktopPane, BorderLayout.CENTER );

    } // frameInit

    /**
     * Create the menu for the frame.
     * <p>
     * @return The menu for the frame.
     **/
    protected JMenuBar createMenu() {
        JMenuBar menuBar = new JMenuBar();

        JMenu file = new JMenu( "File" );
        file.setMnemonic( KeyEvent.VK_F );

        JMenuItem item;

        file.add( new NewInternalFrameAction() );
        file.add( new ExitAction() );

        menuBar.add( file );
        return( menuBar );

    } // createMenuBar

    /**
     * Create the toolbar for this frame.
     * <p>
     * @return The newly created toolbar.
     **/
    protected JToolBar createToolBar() {
        final JToolBar toolBar = new JToolBar();
        toolBar.setFloatable( false );
        toolBar.add( new NewInternalFrameAction() );
        toolBar.add( new ExitAction() );
        return( toolBar );
    }

    /**
     * Create an internal frame.
     * A JLabel is added to its content pane for an example
     * of content in the internal frame. However, any
     * JComponent may be used for content.
     * <p>
     * @return The newly created internal frame.
     **/
    public JInternalFrame createInternalFrame() {

        JInternalFrame internalFrame =
                      new JInternalFrame( "Internal JLabel" );
        internalFrame.getContentPane().add(
                      new JLabel( "Internal Frame Content" ) );
        internalFrame.setResizable( true );
        internalFrame.setClosable( true );
        internalFrame.setIconifiable( true );
        internalFrame.setMaximizable( true );
        internalFrame.pack();

        return( internalFrame );
    }

    /**
     * An Action that creates a new internal frame and
     * adds it to this frame’s desktop pane.
     **/
    public class NewInternalFrameAction extends AbstractAction {

        /**
         * NewInternalFrameAction, constructor.
         * Set the name and icon for this action.
         **/
        public NewInternalFrameAction() {
            super( "New", new ImageIcon( "new.gif" ) );
        }

        /**
         * Perform the action, create an internal frame and
         * add it to the desktop pane.
         * <p>
         * @param e The event causing us to be called.
         **/
        public void actionPerformed( ActionEvent e ) {
            JInternalFrame internalFrame = createInternalFrame();
            desktopPane.add( internalFrame,
                             JLayeredPane.DEFAULT_LAYER );
        }

    } // NewInternalFrameAction

} // MDIFrame


Previous Table of Contents Next
HomeAbout UsSearchSubscribeAdvertising InfoContact UsFAQs
Use of this site is subject to certain Terms & Conditions.
Copyright (c) 1996-1999 EarthWeb Inc. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.