|
To access the contents, click the chapter and section titles.
Platinum Edition Using HTML 4, XML, and Java 1.2
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
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).
Adding Menus to JApplets If youve 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 JApplets 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.
Using Swing-Specific LayoutsIn addition to the AWT layouts that weve been using earlier in this chapter, Swing comes with four layouts of its own:
Taking Advantage of ScrollPaneLayout in JScrollPanesYoull never need to instantiate a ScrollPaneLayout. Instead, just make a new JScrollPane. Youll get the nine areas associated with the ScrollPaneLayout automatically:
Each JViewport has its own layout manager, the ViewportLayout. Working with ViewportLayout and OverlayLayoutLike the ScrollPaneLayout, you dont need to make your own ViewportLayout. You get it automatically with every Viewport. Just add a component to the Viewportthe 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 BoxLayoutThe BoxLayout resembles the AWT FlowLayout, except that you can specify the axiseither 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.
Swing Listeners and EventsRecall 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).
|
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. |