I l@ve RuBoard |
![]() ![]() |
9.2 The JInternalFrame ClassJInternalFrame 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.
Figure 9-2. JInternalFrames in four L&Fs![]() 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 PropertiesJInternalFrame defines the properties and default values shown in Table 9-1. The background and foreground properties are delegated to the frame's content pane.
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 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]
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 EventsJInternalFrame fires an InternalFrameEvent (discussed later in this chapter) whenever the frame's state changes. The following standard methods are provided for working with events:
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 ConstantsTable 9-2 shows the constants defined in this class. They are all strings and contain the names of the bound properties.
9.2.4 ConstructorsJInternalFrame 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.
9.2.5 JLayeredPane MethodsThese methods are applicable only if the frame is contained by a JLayeredPane (otherwise, they do nothing).
9.2.6 Miscellaneous Public Methods
9.2.7 Use of the Glass PaneJInternalFrame 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 PropertyIf 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 ClassJDesktopIcon 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.
9.2.10 The InternalFrameEvent ClassAs 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 ConstantsTable 9-3 shows constants defined as possible values for the event ID.
9.2.11 The InternalFrameListener InterfaceJInternalFrame 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 MethodsAll of these methods, except for internalFrameClosing( ), are called by the JInternalFrame when its properties are changed:
9.2.12 The InternalFrameAdapter ClassThis 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 MethodsThe following methods have empty implementations in this class:
![]() |
I l@ve RuBoard |
![]() ![]() |