9.2 The JInternalFrame Class


JInternalFrame provides the ability to create lightweight frames that exist inside other components. An internal frame is managed entirely within some other Java container, just like any other component, giving the program complete control over iconification, maximization, resizing, etc. Despite looking like "real" windows, the underlying windowing system knows nothing of the existence of internal frames.[1] Figure 9-2 shows what internal frames look like in various L&Fs.

[1] Note that JInternalFrame extends JComponent, not JFrame or Frame, so this statement should seem logical.

Figure 9-2. JInternalFrames in four L&Fs
figs/swng2.0902.gif

There's quite a lot to discuss about JInternalFrames, but most of their power comes when they are used inside a JDesktopPane. This section provides a quick overview of the properties, constructors, and methods available in JInternalFrame, and a more detailed discussion of using internal frames follows.

9.2.1 Properties

JInternalFrame defines the properties and default values shown in Table 9-1. The background and foreground properties are delegated to the frame's content pane.

Table 9-1. JInternalFrame properties

Property

Data type

get

is

set

Default value

accessibleContexto

AccessibleContext

·

   

JInternalFrame.AccessibleJInternalFrame( )

closable

boolean

 

·

·

false

closedb,c

boolean

 

·

·

false

contentPaneb, o

Container

·

 

·

From rootPane

defaultCloseOperation

int

·

 

·

DISPOSE_ON_CLOSE1.3

desktopIcon

JInternalFrame.JDesktopIcon

·

 

·

JInternalFrame.JDesktopIcon( )

desktopPane

JDesktopPane

·

   

null

focusCycleRoot1.4, o

boolean

 

·

·

true (can't be changed)

focusCycleRootAncestor1.4, o

Container

·

   

null (will never change)

frameIconb

Icon

·

 

·

null

glassPaneb, o

Component

·

 

·

From rootPane( )

iconb,c

boolean

 

·

·

false

iconifiable

boolean

 

·

·

false

JMenuBarb,o

JMenuBar

·

 

·

From rootPane( )

layer1.3

int

·

 

·

0 (set available since 1.3)

layeredPaneb,o

JLayeredPane

·

 

·

From rootPane( )

maximizable

boolean

 

·

·

false

maximumb,c

boolean

 

·

·

false

normalBounds1.3

Rectangle

·

 

·

Non-maximized size

resizable

boolean

 

·

·

false

rootPaneb,o

JRootPane

·

   

JRootPane( )

selectedb,c

boolean

 

·

·

false

titleb

String

·

 

·

""

UIb

InternalFrameUI

·

 

·

From L&F

UIClassIDo

String

·

   

"InternalFrameUI"

warningString

String

·

   

null

1.3since 1.3, 1.4since 1.4, bbound, cconstrained, ooverridden

See also properties from the JComponent class (Table 3-6).

Three pairs of properties indicate whether something can be done to a frame and whether that thing is currently done to the frame. They are: closable /closed, iconifiable/icon, and maximizable/maximum. Note that closed, icon, and maximum are constrained properties.

The contentPane , glassPane, layeredPane, and JMenuBar properties come from the RootPaneContainer interface and are taken directly from the frame's JRootPane. The rootPane property is set to a new JRootPane when the frame is constructed.

The value of the defaultCloseOperation property defaults to WindowConstants.DISPOSE_ON_CLOSE. This means that when the user clicks the frame's close widget, the frame is hidden, and its dispose( ) method is called. (Prior to SDK 1.3, the default was HIDE_ON_CLOSE.)

The closed property is one of four constrained properties in this class, which means that attempts to set them could be vetoed. Code that sets these properties must be wrapped in try/catch blocks to handle the potential for a PropertyVetoException. As of this writing, these remain the only constrained properties you'll encounter in the standard Swing classes.

The desktopIcon reflects how the frame is displayed when iconified. A JDesktopIcon (which leaves the rendering to the L&F) is created for the frame when it is instantiated. The desktopPane property provides a convenient way to access the JDesktopPane containing the frame, if there is one.

focusCycleRoot and focusCycleRootAncestor relate to the improved focus traversal mechanism introduced in SDK 1.4 and described in Section 3.5.13. For internal frames the focusCycleRoot property is always true, and focusCycleRootAncestor is always null because an internal frame is always the root of a focus cycle.

frameIcon is the icon painted inside the frame's titlebar (usually on the far left). By default, there is no icon. However, the Basic L&F checks to see if a frameIcon has been set and, if not, paints the "java cup" icon. This explains why an icon appears in the Windows L&F frame shown in Figure 9-2, but not in the others (which provide their own paint( ) implementations, rather than using the one provided by the Basic L&F).[2]

[2] The BasicLookAndFeel is an abstract base class that all the Swing L&Fs extend. For more information, see Chapter 26.

The layer property controls the frame's current layer, if it has been placed in a JLayeredPane. Since SDK 1.3, it has been possible to change a frame's layer by setting this property. The normalBounds property reflects (or changes) the size that the frame occupies when it's not maximized. If the frame is not maximized when getNormalBounds is called, it returns the same value as getBounds.

The resizable property indicates whether the frame can be resized by dragging its edges or corners, and selected indicates whether the frame has been selected (this typically determines the color of the titlebar). selected is a constrained property. title contains the string for the titlebar.

The UI property holds the current L&F implementation for the frame, and UIClassID reflects the class ID for internal frames.

Finally, the warningString property, which is always null, is used to specify the string that should appear in contexts where the frame might be insecure. This is the technique used by java.awt.Window to display a string like "Warning: Applet Window" when a Java window is displayed from an applet. Since JInternalFrames are always fully enclosed by some other top-level container, this property is always null.

9.2.2 Events

JInternalFrame fires an InternalFrameEvent (discussed later in this chapter) whenever the frame's state changes.

The following standard methods are provided for working with events:

public void addInternalFrameListener(InternalFrameListener l)
public void removeInternalFrameListener(InternalFrameListener l)
public InternalFrameListener[] getInternalFrameListeners( ) (since 1.4)

Like all the other Swing classes, JInternalFrame fires PropertyChangeEvents when the value of any bound property is changed. JInternalFrame is unique in that it is the only Swing class that uses vetoable changes for some properties (closed, icon, maximum, and selected).

9.2.3 Constants

Table 9-2 shows the constants defined in this class. They are all strings and contain the names of the bound properties.

Table 9-2. JInternalFrame constants

Constant

Property

CONTENT_PANE_PROPERTY

Indicates that the content pane has changed

FRAME_ICON_PROPERTY

Indicates that the frame's icon has changed

GLASS_PANE_PROPERTY

Indicates that the glass pane has changed

IS_CLOSED_PROPERTY

Indicates that the frame has been opened or closed

IS_ICON_PROPERTY

Indicates that the frame as been iconified or deiconified

IS_MAXIMUM_PROPERTY

Indicates that the frame has been maximized or minimized

IS_SELECTED_PROPERTY

Indicates that the frame has been selected or deselected

LAYERED_PANE_PROPERTY

Indicates that the layered pane has changed

MENU_BAR_PROPERTY

Indicates that the menu bar has changed

ROOT_PANE_PROPERTY

Indicates that the root pane has changed

TITLE_PROPERTY

Indicates that the frame's title has changed

9.2.4 Constructors

JInternalFrame provides constructors that allow several of its boolean properties to be set at creation time. By default, resizable, closable, maximizable, and iconifiable are all set to false.

public JInternalFrame( )
public JInternalFrame(String title)

Create a new frame with all four properties set to false.

public JInternalFrame(String title, boolean resizable)
public JInternalFrame(String title, boolean resizable, boolean closable)
public JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable)
public JInternalFrame(String title, boolean resizable, boolean closable, boolean maximizable, boolean iconifiable)

Allow one to four of the frame's boolean properties to be set at creation time.

9.2.5 JLayeredPane Methods

These methods are applicable only if the frame is contained by a JLayeredPane (otherwise, they do nothing).

public void moveToBack( )
public void toBack( )

Call the containing layered pane's moveToBack( ) method, causing the frame to be the first (bottom) component painted in its layer. toBack( ) just calls moveToBack( ), and the presence of both stems from historical inconsistency in the API.

public void moveToFront( )
public void toFront( )

Call the containing layered pane's moveToFront( ) method, causing the frame to be the last (top) component painted in its layer. These methods are equivalent.

9.2.6 Miscellaneous Public Methods

public void dispose( )

Make the frame invisible, unselected, and closed.

public void doDefaultCloseAction( )

Cause the frame to act exactly as if the Close button had been clicked. Available since 1.3.

public Component getFocusOwner( )

If the frame is active, return the child that has focus (otherwise, return null). Available since 1.3.

public Component getMostRecentFocusOwner( )

If the frame is active, this method returns the same value as getFocusOwner. Otherwise, it returns the value that getFocusOwner would return if the frame were active. Available since 1.4.

public void pack( )

Like Frame's pack( ) method, this method causes the frame to be resized according to the preferred size of its components.

public void reshape(int x, int y, int width, int height)

Call its superclass implementation and then force a repaint of the frame, so that decorations such as the title bar are painted.

public void restoreSubcomponentFocus( )

Request that the frame restore focus to the most recent focus owner. Called by the UI when the frame is reactivated (e.g., by clicking on the title bar). Available since 1.3.

public void show( )

Make the frame visible and select it, bringing it to the front of its layer.

public void updateUI( )

Called to indicate that the L&F for the frame has changed.

9.2.7 Use of the Glass Pane

JInternalFrame is the only Swing class that uses the glass pane (see Section 8.2.2 for a general discussion). To be precise, JInternalFrame itself doesn't do anything special with the glass pane, but the default UI implementation (BasicInternalFrameUI) does. This class toggles the visibility of an internal frame's glass pane each time the state of the frame's selected property changes. When the frame is selected, the glass pane is made invisible, allowing components inside the frame to be accessed with the mouse. But when the frame is not selected, the glass pane is made visible. This means that the first time you click anywhere within an unselected internal frame, the mouse click does not get through to the component within the frame that you clicked on, but is instead intercepted by the glass pane, causing the frame to be selected (and causing the glass pane to be removed).

9.2.8 The Metal Look-and-Feel JInternalFrame.isPalette Client Property

If you plan to use the Metal L&F in your application, you can take advantage of a special custom property supported by MetalInternalFrameUI. This client property allows you to define an internal frame as a palette. This effectively amounts to removing the thick border from the frame. This is a technique commonly used in word-processing or graphics-editing programs to provide small windows that contain a set of convenient edit buttons. If you couple the use of this client property with the use of the desktop's PALETTE_LAYER (discussed later), you have a nice borderless frame that floats above your other internal frames. Here's an example of how you'd use this property:

JInternalFrame palette = new JInternalFrame( ); // Use any constructor. palette.putClientProperty("JInternalFrame.isPalette", Boolean.TRUE); palette.setBounds(0, 0, 50, 150); JDesktopPane desk = new JDesktopPane( ); desk.add(palette, JDesktopPane.PALETTE_LAYER);

Other L&Fs quietly ignore this property. (If you'd like a framework in which to try out this code, there is a full-blown example program for working with internal frames at the end of this chapter that you can use as a starting point.)

9.2.9 The JInternalFrame.JDesktopIcon Class

JDesktopIcon is a static inner class of JInternalFrame that provides an iconified view of a frame. JInternalFrame instantiates a JDesktopIcon when the frame is created. The class extends JComponent and, like other Swing components, leaves all details of its visual appearance to its UI delegate.

Note that this class has no relation at all to the Swing Icon interface.

You should not work with the JDesktopIcon class directly the Javadoc for this inner class indicates that it will go away in a future Swing release.

9.2.10 The InternalFrameEvent Class

As we described earlier in the chapter, JInternalFrames fire InternalFrameEvents when the state of the frame changes. These are standard AWTEvent subclasses, providing a number of constants to define the type of change that was made to the frame. Since SDK 1.3, it also provides a getInternalFrame method to retrieve the associated frame.

9.2.10.1 Constants

Table 9-3 shows constants defined as possible values for the event ID.

Table 9-3. InternalFrameEvent constants

Constant

Type

Description

INTERNAL_FRAME_ACTIVATED

int

The frame has been activated, typically causing the title bar to change to a special color and the frame to gain focus.

INTERNAL_FRAME_CLOSED

int

The frame has been closed (sent anytime the frame is closed).

INTERNAL_FRAME_CLOSING

int

The frame is about to be closed (sent when the user clicks the closebox on the frame).

INTERNAL_FRAME_DEACTIVATED

int

The frame has been deactivated, typically causing the title bar to change to a default color and the frame to lose focus.

INTERNAL_FRAME_DEICONIFIED

int

The frame has been restored from an icon.

INTERNAL_FRAME_ICONIFIED

int

The frame has been iconified.

INTERNAL_FRAME_OPENED

int

The frame has been opened.

INTERNAL_FRAME_FIRST

int

The first integer value used to represent the above event IDs.

INTERNAL_FRAME_LAST

int

The last integer value used to represent the above event IDs.

9.2.11 The InternalFrameListener Interface

JInternalFrame fires InternalFrameEvents to registered InternalFrameListeners. This interface defines the following set of methods (which have a one-to-one correspondence to the methods in the java.awt.event.WindowListener interface).

9.2.11.1 Methods

All of these methods, except for internalFrameClosing( ), are called by the JInternalFrame when its properties are changed:

public abstract void internalFrameActivated(InternalFrameEvent e)

The frame has been activated, typically meaning that it gains focus and is brought to the front.

public abstract void internalFrameClosed(InternalFrameEvent e)

The frame has been closed.

public abstract void internalFrameClosing(InternalFrameEvent e)

The frame is closing. This is called by the L&F when the close button is clicked.

public abstract void internalFrameDeactivated(InternalFrameEvent e)

The frame has been deactivated.

public abstract void internalFrameDeiconified(InternalFrameEvent e)

The frame has been restored from an icon.

public abstract void internalFrameIconified(InternalFrameEvent e)

The frame has been reduced to an icon.

public abstract void internalFrameOpened(InternalFrameEvent e)

A previously closed frame has been opened.

9.2.12 The InternalFrameAdapter Class

This class follows the standard AWT 1.1 listener/adapter pattern by providing empty implementations of the seven methods defined in the InternalFrameListener interface. If you are interested only in certain types of events, you can create a subclass of this adapter that implements only the methods you care about.

9.2.12.1 Methods

The following methods have empty implementations in this class:

public void internalFrameActivated(InternalFrameEvent e)
public void internalFrameClosed(InternalFrameEvent e)
public void internalFrameClosing(InternalFrameEvent e)
public void internalFrameDeactivated(InternalFrameEvent e)
public void internalFrameDeiconified(InternalFrameEvent e)
public void internalFrameIconified(InternalFrameEvent e)
public void internalFrameOpened(InternalFrameEvent e)


Java Swing
Graphic Java 2: Mastering the Jfc, By Geary, 3Rd Edition, Volume 2: Swing
ISBN: 0130796670
EAN: 2147483647
Year: 2001
Pages: 289
Authors: David Geary

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