Click Here!
home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Platinum Edition Using HTML 4, XML, and Java 1.2
(Publisher: Macmillan Computer Publishing)
Author(s): Eric Ladd
ISBN: 078971759x
Publication Date: 11/01/98

Bookmark It

Search this book:
 
Previous Table of Contents Next


JRootPane enables you to place contents into one of several layers. In order from front (closest to user) to back (farthest from user) the layers are

1.  glassPane—A JComponent that fills the entire viewable area of the JRootPane. By default, the glassPane is not visible.
2.  layeredPane—A subclass of JComponent designed to hold dialog boxes, menu pop-ups, and other components that should appear to be floating between the user and the content.
3.  menubar—An optional component; if present, it appears anchored to the top of the JRootPane.
4.  contentPane—The JComponent where most of the contents will be drawn.

Using the contentPane To add components to a JApplet, you should usually add them to the contentPane. Instead of writing

theApplet.add(theComponent);

as you would in AWT, write

theApplet.getContentPane().add(theComponent);

Working with the JLayeredPane By placing most of your components in the contentPane (which is farthest away from the user), you make it possible to add special components such as menu pop-ups or dialog boxes in a layer closer to the user (such as the layeredPane).


NOTE:  By default, the contentPane has a BorderLayout layout manager.


NOTE:  If you plan to use the layeredPane, be sure to read the JDK documentation on javax.swing.JLayeredPane. This class—a layer in itself—supports six distinct layers internally: from back to front, the FRAME_CONTENT_LAYER, the DEFAULT_LAYER, the PALETTE_LAYER, the MODAL_LAYER, the POPUP_LAYER, and the DRAG_LAYER. In addition, you can make up layers of your own.

Adding Menus to JApplets If you’ve worked with the AWT, you know that you cannot easily add menus to an applet. AWT menus need to be attached to a Frame, but an Applet is a Panel. With JApplet, you can set a JMenuBar on the JRootPane. It will be positioned along the upper edge of the JApplet’s JRootPane.

Drawing on the glassPane The glassPane is closest to the user. If you need to draw something that should appear in front of all components, including dialog boxes, menu pop-ups, and other components on the layeredPane, add it to the glassPane.


If you use the glassPane, remember to make it visible. It is not visible by default. In a JApplet, for example, you might write
getGlassPane.setVisible(TRUE);

Using Swing-Specific Layouts

In addition to the AWT layouts that we’ve been using earlier in this chapter, Swing comes with four layouts of its own:

  ScrollPaneLayout—Built into the ScrollPane component.
  ViewportLayout—Built into the Viewport component.
  BoxLayout—Built into the Box component, but also available as an option in other components.
  OverlayLayout—A layout manager in which every component is added on top of every previous component.

Taking Advantage of ScrollPaneLayout in JScrollPanes

You’ll never need to instantiate a ScrollPaneLayout. Instead, just make a new JScrollPane. You’ll get the nine areas associated with the ScrollPaneLayout automatically:

  A JViewport, in the center—Use it for your contents.
  Two JScrollBars—One for horizontal scrolling, the other for vertical scrolling.
  Two JViewPorts—One for row headers, the other for column headers.
  Four Components—One for each corner.


NOTE:  ScrollPaneLayout includes named constants to make it easy for you to refer to the parts of the layout. The corners, for example, are named LOWER_LEFT_CORNER, LOWER_RIGHT_CORNER, UPPER_LEFT_CORNER, and UPPER_RIGHT_CORNER.

Each JViewport has its own layout manager, the ViewportLayout.

Working with ViewportLayout and OverlayLayout

Like the ScrollPaneLayout, you don’t need to make your own ViewportLayout. You get it automatically with every Viewport. Just add a component to the Viewport—the ViewportLayout will position it based on the properties of your Viewport.

The OverlayLayout positions each component over the top of the others. The size of the complete layout is the size of the largest component.

Using Box and BoxLayout

The BoxLayout resembles the AWT FlowLayout, except that you can specify the axis—either x or y. Unlike GridLayout, each component can occupy a different size cell. To use the BoxLayout in the y axis, write

setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

Figure 38.24 illustrates a BoxLayout in the y axis.


FIGURE 38.24  This BoxLayout is set up for the y axis and has three components.


If you plan to use BoxLayout, just subclass Box rather than JPanel. BoxLayout is the default layout manager for Box and provides several methods that give you detailed control over the layout.

Swing Listeners and Events

Recall from the earlier sections of this chapter, “Using the Abstract Windowing Toolkit” and “The Swing Architecture,” that one of the distinctives of the JFC is the delegation event model. Although the AWT components can still use the now-deprecated JDK-1.0 model for communicating about actions, Swing components use only the delegation event model. In fact, Swing takes the delegation event model to new heights, based on the Model-View-Controller (MVC) design pattern.

• To learn more about MVC, see Chapter 13 of Using Java 1.2 (Que, 1998).


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.