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


The OKDialog constructor instantiates a button and a label and adds them to the dialog, then calls pack() to shrink the dialog window down to a size that contains the components. The listener that is attached to the dialog’s button makes the dialog window invisible.


NOTE:  You may have noticed that the calls to add() in OKDialog() include strings. For example, we add the message label by saying

add(theMessageLabel, BorderLayout.NORTH);

These strings are part of the BorderLayout, which is described in the next section.


Understanding theChoice In Java, a Choice is a pop-up list that displays one item at a time. We add a Choice (called theChoice—it’s a local variable, not an instance variable) and a matching Label. Then we add an ItemListener. When we get an itemStateChanged() event, we use getItemSelectable() to get a reference back to theChoice and then call getSelectedObjects() to get an array of selected items. If theChoice is working correctly, we should be able to get back only a single item. (We throw an exception if that condition doesn’t hold.) Finally, we modify fLabelForChoice to reflect the user’s choice.

Working with Layouts

Recall that several of the sample applets shown in Chapter 37 use layout managers such as CardLayout or GridLayout to manage the placement of components on the screen. You can see the effect of a layout manager by resizing the HelloPlus window. Figure 38.6 shows a maximized window in which the components have room to spread out; in Figure 38.7, the window is tall and narrow—the components appear one above the other.


FIGURE 38.6  When the components have enough room, they’re laid out one beside the other.


FIGURE 38.7  When the window is narrow, the components flow one above the next.

You don’t have to worry about the precise location of every component or whether the user is working on a 640 × 480 screen or an older Macintosh with a 9-inch monitor. That’s because every Java window has an associated LayoutManager. If you don’t specify the manager, Java supplies one for you. This section walks you through the standard layouts:

  FlowLayout—The components are added left to right, top to bottom. This layout is the default layout for Panel and its derived classes, including Applet.
  GridLayout—Similar to FlowLayout, but each component gets an equal-sized cell.
  BorderLayout—Divides the container into five areas: North, South, East, West, and Center. This layout is the default layout for Windows (except special-purpose Windows such as FileDialog).
  CardLayout—Used to display one component at a time, like a stack of index cards.
  GridBagLayout—A flexible (and complicated) layout used when none of the other layout managers will do.


NOTE:  By default, Java supplies a layout manager for every new Container. It’s possible to turn it off (by calling setLayout(null)) and then use absolute positioning to lay out your components. That’s a poor idea, though—your application won’t display correctly on different-sized monitors.

If you find that none of the standard layout managers meet your needs, you can make your own implementation of LayoutManager2. (LayoutManager, a simpler class, doesn’t support a constraints object, so it’s less useful.) You’ll find details of this process in Chapter 11 of Using Java 1.2 (Que, 1997).

Using FlowLayout

Figures 38.6 and 38.7 showed how HelloPlus.java looks when you use the default layout for Panel, FlowLayout. (Recall that an Applet is a kind of Panel.) If you’re happy with this design—all the components laid out left to right, top to bottom, with each component taking up whatever amount of space it requires—you don’t have to do anything. Just call add() to place each component into the Container.

If you’re working in a Window, where the default layout is BorderLayout, you’ll need to change the layout manager if you want a FlowLayout. Just write

setLayout(new FlowLayout());

before you add() any components.

Using GridLayout

Figure 38.8 shows how HelloPlus looks in a GridLayout. A GridLayout is similar to a FlowLayout, but each component gets the same size cell as all the others. The cell size is determined by the size of the largest component. To apply a GridLayout to HelloPlus, add this line to the top of init():

setLayout(new GridLayout(rows, columns));

where rows gives the number of rows and columns gives the number of columns. If you set either value to zero, Java will use as many of that dimension as necessary to display all the components in the layout.


NOTE:  If you specify both the number of rows and the number of columns and then add more components than the product of those two numbers, the layout manager will behave as though you had specified a zero for the number of columns. I don’t recommend that you rely upon this feature—it’s not documented, so Sun could change the behavior in the future.


FIGURE 38.8  This grid was produced with rows=0 and columns=2.

Using BorderLayout

BorderLayout is particularly appropriate when you have one large component and several smaller ones because you can place the large component in the center position. Figure 38.9 illustrates the general design of BorderLayout, and Figure 38.10 shows HelloPlus.java in this layout.


FIGURE 38.9  Think of BorderLayout as a compass, with the largest component in the center.


FIGURE 38.10  You need to put some components into panels to lay out more than five components in a BorderLayout.

To modify HelloPlus.java to use a BorderLayout, you need to make three changes:

  Set the layout to BorderLayout in init().
  Assemble some of the components into groups on Panels.
  Change add() so that it passes the location String.


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.