close this bookExploring Java:By Patrick Niemeyer & Joshua Peck
source ref: ebookjava.html
View the documentMetadata
View the documentPreface
View the documentChapter 1: Yet Another Language?
View the documentChapter 2: A First Applet
View the documentChapter 3: Tools of the Trade
View the documentChapter 4: The Java Language
View the documentChapter 5: Objects in Java
View the documentChapter 4: The Java Language
View the documentChapter 6: Threads
View the documentChapter 7:Basic Utility Classes
View the documentChapter 8:Input/Output Facilities
View the documentChapter 9:Network Programming
View the documentChapter 10:Understand the Abstract Windowing Toolkit
View the documentChapter 11:Using and Creating GUI Components
View the documentChapter 12:Layout Managers
View the documentChapter 13:Drawing With the AWT
View the documentChapter 14:Working With Images

Chapter 12:Layout Managers

 

 

12. Layout Managers

 

A layout manager arranges the child components of a container, as shown in Figure 12.1. It positions and sets the size of components within the container's display area according to a particular layout scheme. The layout manager's job is to fit the components into the available area, while maintaining the proper spatial relationships between the components. AWT comes with a few standard layout managers that will collectively handle most situations; you can make your own layout managers if you have special requirements.

Every container has a default layout manager; therefore, when you make a new container, it comes with a LayoutManager object of the appropriate type. You can install a new layout manager at any time with the setLayout() method. Below, we set the layout manager of a container to a BorderLayout:

setLayout ( new BorderLayout() ); 

Notice that we call the BorderLayout constructor, but we don't even save a reference to the layout manager. This is typical; once you have installed a layout manager, it does its work behind the scenes, interacting with the container. You rarely call the layout manager's methods directly, so you don't usually need a reference (a notable exception for CardLayout). However, you do need to know what the layout manager is going to do with your components as you work with them.

As I explained earlier, the LayoutManager is consulted whenever a container's doLayout() method is called (usually when it is validated), to reorganize the contents. It does its job by calling the setLocation() and setBounds() methods of the individual child components to arrange them in the container's display area. A container is layed out the first time it is displayed, and thereafter whenever the container's validate() method is called. Containers that are a subclass of the Window class (which include Frame) are automatically validated whenever they are packed or resized. Calling pack() sets the window's size so it is as small as possible while holding all its components at their preferred sizes.

Every component determines three important pieces of information used by the layout manager in placing and sizing it: a minimum size, a maximum size, and a preferred size. These are reported by the getMinimumSize(), getMaximumSize(), and getPreferredSize(), methods of Component, respectively. For example, a plain Button object can normally be sized to any proportions. However, the button's designer can provide a preferred size for a good-looking button. The layout manager might use this size when there are no other constraints, or it might ignore it, depending on its scheme. Now, if we give the button a label, the button may need a minimum size in order to display itself properly. The layout manager might show more respect for the button's minimum size and guarantee that it has at least that much space. Similarly, a particular component might not be able to display itself properly if it is too large (perhaps it has to scale up an image); it can use getMaximumSize() to report the largest size it considers acceptable.[1]

[1] Unfortunately, the current set of layout managers doesn't do anything with the maximum size. This may change in a future release.

The preferred size of a Container object has the same meaning as for any other type of component. However, since a Container may hold its own components and want to arrange them in its own layout, its preferred size is a function of its layout manager. The layout manager is therefore involved in both sides of the issue. It asks the components in its container for their preferred (or minimum) sizes in order to arrange them. Based on those values it also calculates the preferred size that is reported by its own container to that container's parent.

When a layout manager is called to arrange its components, it is working within a fixed area. It usually begins by looking at its container's dimensions, and the preferred or minimum sizes of the child components. It then doles out screen area and sets the sizes of components according to its scheme. You can override the getMinimumSize(), getMaximumSize(), and getPreferredSize() methods of a component, but you should do this only if you are actually specializing the component, and it has new needs. If you find yourself fighting with a layout manager because it's changing the size of one of your components, you are probably using the wrong kind of layout manager or not composing your user interface properly. Remember that it's possible to use a number of Panel objects in a given display, where each one has its own LayoutManager. Try breaking down the problem: place related components in their own Panel and then arrange the panels in the container. When that becomes too complicated, you can choose to use a constraint based layout manager like GridBagLayout, which we'll discuss later in this chapter.

12.1 FlowLayout

FlowLayout is a simple layout manager that tries to arrange components with their preferred sizes, from left to right and top to bottom in the display. A FlowLayout can have a specified justification of LEFT, CENTER, or RIGHT, and a fixed horizontal and vertical padding. By default, a flow layout uses CENTER justification, meaning that all components are centered within the area allotted to them. FlowLayout is the default for Panel components like Applet.

The following applet adds five buttons to the default FlowLayout; the result is shown in Figure 12.2.

import java.awt.*; 
 
public class Flow extends java.applet.Applet {  
    public void init() { 
        // Default for Applet is FlowLayout 
        add( new Button("One") ); 
        add( new Button("Two") ); 
        add( new Button("Three") ); 
        add( new Button("Four") ); 
        add( new Button("Five") ); 
    } 
} 

If the applet is small enough, some of the buttons spill over to a second or third row.

12.2 GridLayout

GridLayout arranges components into regularly spaced rows and columns. The components are arbitrarily resized to fit in the resulting areas; their minimum and preferred sizes are consequently ignored. GridLayout is most useful for arranging very regular, identically sized objects and for allocating space for Panels to hold other layouts in each region of the container.

GridLayout takes the number of rows and columns in its constructor. If you subsequently give it too many objects to manage, it adds extra columns to make the objects fit. You can also set the number of rows or columns to zero, which means that you don't care how many elements the layout manager packs in that dimension. For example, GridLayout(2,0) requests a layout with two rows and an unlimited number of columns; if you put ten components into this layout, you'll get two rows of five columns each.

The following applet sets a GridLayout with three rows and two columns as its layout manager; the results are shown in Figure 12.3.

import java.awt.*; 
 
public class Grid extends java.applet.Applet {  
    public void init() { 
        setLayout( new GridLayout( 3, 2 )); 
        add( new Button("One") ); 
        add( new Button("Two") ); 
        add( new Button("Three") ); 
        add( new Button("Four") ); 
        add( new Button("Five") ); 
    } 
} 

The five buttons are laid out, in order, from left to right, top to bottom, with one empty spot.

 

 

12.3 BorderLayout

BorderLayout is a little more interesting. It tries to arrange objects in one of five geographical locations: "North," "South," "East," "West," and "Center," possibly with some padding between. BorderLayout is the default layout for Window and Frame objects. Because each component is associated with a direction, BorderLayout can manage at most five components; it squashes or stretches those components to fit its constraints. As we'll see in the second example, this means that you often want to have BorderLayout manage sets of components in their own panels.

When we add a component to a border layout, we need to specify both the component and the position at which to add it. To do so, we use an overloaded version of the add() method that takes an additional argument as a constraint. This additional argument is passed to the layout manager when the new component is added. In this case it specifies the name of the position for the BorderLayout. Otherwise the LayoutManager is not consulted until it's asked to lay out the components.

The following applet sets a BorderLayout layout and adds our five buttons again, named for their locations; the result is shown in Figure 12.4.

Figure 12.4: A border layout

[Graphic: Figure 12-4]

import java.awt.*; 
 
public class Border extends java.applet.Applet {  
    public void init() { 
        setLayout( new java.awt.BorderLayout() ); 
        add( new Button("North"), "North" ); 
        add( new Button("East"), "East" ); 
        add( new Button("South"), "South" ); 
        add( new Button("West"), "West" ); 
        add( new Button("Center"), "Center" ); 
    } 
} 

So, how exactly is the area divided up? Well, the objects at "North" and "South" get their preferred height and are expanded to fill the full area horizontally. "East" and "West" components on the other hand, get their preferred width, and are expanded to fill the remaining area between "North" and "South" vertically. Finally, the "Center" object takes all of the rest of the space. As you can see in Figure 12.5, our buttons get distorted into interesting shapes.

What if we don't want BorderLayout messing with the sizes of our components? One option would be to put each button in its own Panel. The default layout for a Panel is FlowLayout, which respects the preferred size of components. The preferred sizes of the panels are effectively the preferred sizes of the buttons, but if the panels are stretched, they won't pull their buttons with them. Border2 illustrates this approach as shown in Figure 12.5.

import java.awt.*; 
 
public class Border2 extends java.applet.Applet {  
    public void init() { 
        setLayout( new BorderLayout() ); 
        Panel p = new Panel(); 
        p.add(new Button("East") ); 
        add( p, "East" ); 
        p = new Panel(); 
        p.add(new Button("West") ); 
        add( p, "West" ; 
        p = new Panel(); 
        p.add(new Button("North") ); 
        add( p, "North" ); 
        p = new Panel(); 
        p.add(new Button("South") ); 
        add(p, "South" ); 
        p = new Panel(); 
        p.add(new Button("Center") ); 
        add( p, "Center" ); 
    } 
} 

In this example, we create a number of panels, put our buttons inside the panels, and put the panels into the applet, which has the BorderLayout manager. Now, the Panel for the "Center" button soaks up the extra space that comes from the BorderLayout. Each Panel's FlowLayout centers the button in the panel and uses the button's preferred size. In this case, it's all a bit awkward. (This is one of the problems that getMaximumSize() will eventually solve.) We'll see how we could accomplish this more directly using GridBagLayout shortly.

Finally, this version of the applet has a lot of unused space. If we wanted, we could get rid of the extra space by resizing the applet:

setSize( getPreferredSize() ); 

12.4 CardLayout

CardLayout is a special layout manager for creating the effect of a stack of cards. Instead of arranging all of the container's components, it displays only one at a time. You would use this kind of layout to implement a hypercard stack or a Windows-style set of configuration screens. When you add a component to the layout, you use the two-argument version of add(); the extra argument is an arbitrary string that serves as the card's name:

add("netconfigscreen", myComponent); 

To bring a particular card to the top of the stack, call the CardLayout's show() method with two arguments: the parent Container and the name of the card you want to show. There are also methods like first(), last(), next(), and previous() for working with the stack of cards. These methods take a single argument: the parent Container. Here's a simple example:

import java.awt.*; 
      
public class main extends java.applet.Applet { 
    CardLayout cards = new CardLayout(); 
      
    public void init() { 
        setLayout( cards ); 
        add( new Button("one"), "one" ); 
        add( new Button("two"), "two" ); 
        add( new Button("three"), "three" ); 
    } 
      
    public boolean action( Event e, Object arg) { 
        cards.next( this ); 
        return true; 
    } 
} 

We add three buttons to the layout and cycle through them as they are pressed. In a more realistic example, we would build a group of panels, each of which might implement some part of a complex user interface, and add those panels to the layout. Each panel would have its own layout manager. The panels would be resized to fill the entire area available (i.e., the area of the Container they are in), and their individual layout managers would arrange their internal components.

 

 

12.5 GridBagLayout

GridBagLayout is a very flexible layout manager that allows you to position components relative to one another using constraints. With GridBagLayout (and a fair amount of effort) you can create almost any imaginable layout. Components are arranged at "logical" coordinates on a abstract grid. We'll call them "logical" coordinates because they really designate positions in the space of rows and columns formed by the set of components. Rows and columns of the grid stretch to different sizes, based on the sizes and constraints of the components they hold.

A row or column in a GridBagLayout expands to accommodate the dimensions and constraints of the largest component in its ranks. Individual components may span more than one row or column. Components that aren't as large as their grid cell can be "anchored" within their "cell." They can also be set to fill or expand their area in either dimension. Extra area in the grid rows and columns can be parceled out according to the weight constraints of the components. Therefore, you can control how various components will grow and stretch when a window is resized.

GridBagLayout is much easier to use in a graphical, WYSIWYG GUI builder environment. That's because working with GridBag is kind of like messing with the "rabbit ears" antennae on your television. It's not particularly difficult to get the results that you want through trial and error, but writing out hard and fast rules for how to go about it is difficult. In short, GridBagLayout is complex and has some quirks. It is also simply a bit ugly both in model and implementation. Remember that you can do a lot with nested panels and by composing simpler layout managers within one another. If you look back through this chapter, you'll see many examples of composite layouts; it's up to you to determine how far you should go before making the break from simpler layout managers to a more complex "do it all in one" layout manager like GridBagLayout.

GridBagConstraints

Having said that GridBagLayout is complex and a bit ugly, I'm going to contradict myself and say that it's surprisingly simple. There is only one constructor with no arguments (GridBagLayout()), and there aren't a lot of fancy methods to control how the display works.

The appearance of a grid bag layout is controlled by sets of GridBagConstraints, and that's where things get hairy. Each component managed by a GridBagLayout is associated with a GridBagConstraints object. GridBagConstraints holds the following variables, which we'll describe in detail shortly:

int gridx, gridy

Controls the position of the component on the layout's grid.

int weightx, weighty

Controls how additional space in the row or column is allotted to the component.

int fill

Controls whether the component expands to fill the space allocated to it.

int gridheight, gridwidth

Controls the number of rows or columns the component occupies.

int anchor

Controls the position of the component if there is extra room within the space allocated for it.

int ipadx, ipady

Controls padding between the component and the borders of it's area.

Insets insets

Controls padding between the component and neighboring components.

To make a set of constraints for a component or components, you simply create a new instance of GridBagConstraints and set these public variables to the appropriate values. There are no pretty constructors, and not much else to the class at all.

The easiest way to associate a set of constraints with a component is to use the version of add() that takes a layout object as an argument, in addition to the component itself. This puts the component in the container and associates your GridBagConstraints object with it.

    Component component = new Label("constrain me, please...");
    GridBagConstraints constraints = new GridBagConstraints;
    constraints.gridx = x;
    constraints.gridy = y;
    ...
    add( component, constraints );

You can also add a component to a GridBagLayout using the single argument add() method, and then later calling the layout's setConstraints() method directly, to pass it the GridBagConstraints object for that component.

    add( component );
    ...
    myGridBagLayout.setConstraints( component, constraints );

In either case, the set of constraints is copied when it is applied to the component. Therefore, you're free to create a single set of GridBagConstraints, modify it as needed, and apply it as needed to different objects. You might find it helpful to create a helper method that sets the constraints appropriately, then adds the method with its constraints to the layout. That's the approach we'll take in our examples; our helper method is called addGB(), and it takes a component plus a pair of coordinates as arguments. These coordinates become the gridx and gridy values for the constraints. We could expand upon this later and overload addGB() to take more parameters for other constraints that we often change from component to component.

Grid Coordinates

One of the biggest surprises in the GridBagLayout is that there's no way to specify the size of the grid. There doesn't have to be. The grid size is determined implicitly by the constraints of all the objects; the layout manager picks dimensions large enough so that everything fits. Thus, if you put one component in a layout and set its gridx and gridy constraints to 25, the layout manager creates a 25 x 25 grid, with rows and columns both numbered from 0 to 24. If you add a second component with a gridx of 30 and a gridy of 13, the grid's dimensions change to 30 x 25. You don't have to worry about setting up an appropriate number of rows and columns. The layout manager does it automatically, as you add components.

With this knowledge, we're ready to create some simple displays. We'll start by arranging a group of components in a cross shape. We maintain explicit x and y local variables, setting them as we add the components to our grid. This is partly for clarity, but it can be a handy technique when you want to add a number of components in a row or column. You can simply increment gridx or gridy before adding each component. This is a simple and problem-free way to achieve relative placement. (Later, we'll describe GridBagConstraints's RELATIVE constant, which does relative placement automatically.) Here's our first layout:

import java.awt.*;
public class GridBag1 extends java.applet.Applet {
    GridBagConstraints constraints = new GridBagConstraints();
    void addGB( Component component, int x, int y  ) {
        constraints.gridx = x;  
        constraints.gridy = y;
        add ( component, constraints );
    }
    
    public void init() {
        setLayout( new GridBagLayout() );
        int x, y;  // for clarity
        addGB( new Button("North"),  x=1,y=0 );
        addGB( new Button("West"),   x=0,y=1 );
        addGB( new Button("Center"), x=1,y=1 );
        addGB( new Button("East"),   x=2,y=1 );
        addGB( new Button("North"),  x=1,y=2 );
    }
}

You probably noticed that the buttons in this example are "clumped" together in the center of their display area. Each button is displayed at its preferred size, without stretching the button to fill the available space. This is how the layout manager behaves when the "weight" constraints are left unset. We'll talk more about weights in the next two sections.

Fill

Now let's make the buttons expand to fill the entire applet. To do so, we must take two steps: we must set the fill constraint for each button to the value BOTH, and we must set the weightx and weighty values to the same nonzero value. Here's the resulting layout, followed by the applet:

    public void init() {
        setLayout( new GridBagLayout() );
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.fill = GridBagConstraints.BOTH;
        int x, y;  // for clarity
        addGB( new Button("North"),  x=1,y=0 );
        addGB( new Button("West"),   x=0,y=1 );
        addGB( new Button("Center"), x=1,y=1 );
        addGB( new Button("East"),   x=2,y=1 );
        addGB( new Button("North"),  x=1,y=2 );
    }

BOTH is one of the constants of the GridBagConstraints class; it tells the component to fill the available space in both directions. The following table lists the constants that you can use to set the fill field:

HORIZONTAL

Fill the available horizontal space.

VERTICAL

Fill the available vertical space.

BOTH

Fill the available space in both directions.

NONE

Don't fill the available space; display the component at its preferred size.

We set the weight constraints to 1.0; it doesn't matter what they are, provided that each component has the same non-zero weight. fill doesn't work if the component weights in the direction you're filling are 0, which is the default value.

Weighting

The weightx and weighty variables of a GridBagConstraints object determine how space is distributed among the columns or rows in a layout. As long as you keep things simple, the effect these variables have is fairly intuitive: the larger the weight, the greater the amount of space allocated to the component. The display below shows what happens if we vary the weightx constraint from 0.1 to 1.0 as we place three buttons in a row.

    public void init() {
        setLayout( new GridBagLayout() );
        constraints.fill = GridBagConstraints.BOTH;
        constraints.weighty = 1.0;
        int x, y; // for clarity
        constraints.weightx = 0.1;
        addGB( new Button("one"),   x=0, y=0 );
        constraints.weightx = 0.5;
        addGB( new Button("two"),   ++x, y );
        constraints.weightx = 1.0;
        addGB( new Button("three"), ++x, y );
    }

Although the weight values have no real meaning, you might find it convenient to use values that add up to 1. When you're working with weights, it is best not to get complicated. The effect of combining weights with different padding values can be very strange, as can be the effect of using different weightx values for components in the same column, or different weighty values for components in the same row. While it's possible to examine the code for the GridBagLayout and figure out exactly what it will do in any given situation, it really isn't worth the effort.

Spanning rows and columns

Perhaps the best feature of the GridBaglayout is that it lets you create arrangements in which components span two or more rows or columns. To do so, you set the gridwidth and gridheight variables of the GridBagConstraints. Here's an applet that creates such a display; button one spans two columns vertically, and button four spans two horizontally.

    public void init() {
        setLayout( new GridBagLayout() );
        constraints.weightx = 1.0;
        constraints.weighty = 1.0;
        constraints.fill = GridBagConstraints.BOTH;
        int x, y;  // for clarity
        constraints.gridheight = 2; // Span two rows
        addGB( new Button("one"),   x=0, y=0 );
        constraints.gridheight = 1; // set it back
        addGB( new Button("two"),   x=1, y=0 );
        addGB( new Button("three"), x=2, y=0 );
        constraints.gridwidth = 2; // Span two columns
        addGB( new Button("four"),  x=1, y=1 );
        constraints.gridwidth = 1; // set it back
    }

The size of each element is controlled by the gridwidth and gridheight values of its constraints. For button one, we set gridheight to 2. Therefore, it is two cells high; its gridx and gridy positions are both zero, so it occupies cell (0,0) and the cell directly below it, (0,1). Likewise, button four has a gridwidth of 2 and a gridheight of 1, so it occupies two cells horizontally. We place this button in cell (1,1), so it occupies that cell and its neighbor, (2,1).

In this example, we set the fill to BOTH, and weightx and weighty to 1, for all components. By doing so, we told each button to occupy all the space available. Strictly speaking, this isn't necessary. However, it makes it easier to see exactly how much space each button occupies.

Anchoring

If a component is smaller than the space available for it, it is centered by default. But centering isn't the only possibility. The anchor constraint tells a grid bag layout how to position a component within its space. Possible values are: GridBagConstraints.CENTER, NORTH, NORTHEAST, EAST, SOUTHEAST, SOUTH, SOUTHWEST, WEST, and NORTHWEST. For example, an anchor of GridBagConstraints.NORTH centers a component at the top of its display area; SOUTHEAST places a component at the bottom left of its area.

Padding and Insets

Another way to control the behavior of a component in a grid bag layout is to use padding and insets. Padding is determined by the ipadx and ipady fields of GridBagConstraints. They specify additional horizontal and vertical space that is added to the component when it is placed in its cell. In the example below, the West button is larger because we have set the ipadx and ipady values of its constraints to 25. Therefore, the layout manager gets the button's preferred size and adds 25 pixels in each direction to determine the button's actual size. The sizes of the other buttons are unchanged because their padding is set to 0 (the default), but their spacing is different. The West button is unnaturally tall, which means that the middle row of the layout must be taller than the others.

    public void init() {
        setLayout( new GridBagLayout() );
        int x, y;  // for clarity
        addGB( new Button("North"),  x=1,y=0 );
        constraints.ipadx = 25;  // set padding
        constraints.ipady = 25;
        addGB( new Button("West"),   x=0,y=1 );
        constraints.ipadx = 0;   // set padding back
        constraints.ipady = 0;
        addGB( new Button("Center"), x=1,y=1 );
        addGB( new Button("East"),   x=2,y=1 );
        addGB( new Button("North"),  x=1,y=2 );
    }

Notice that the horizontal padding, ipadx, is added on both the left and right sides of the button. Therefore, the button grows horizontally by twice the value of ipadx. Likewise, the vertical padding, ipady, is added on both the top and the bottom.

Insets add space between the edges of the component and its cell. They are stored in the insets field of GridBagConstraints, which is an Insets object. An Insets object has four fields, to specify the margins on the top, bottom, left, and right of the component. The relationship between insets and padding can be confusing. As shown in the following diagram, padding is added to the component itself, increasing its size. Insets are external to the component and represent the margin between the component and its cell.

Padding and weighting have an odd interaction with each other. If you use padding, it is best to use the default weightx and weighty values for each component.

Relative Positioning

In all of our grid bag layouts so far, we have specified the gridx and gridy coordinates of each component explicitly using its constraints. There's another alternative: relative positioning.

Conceptually, relative positioning is simple: we simply say "put this component to the left of (or below) the previous component." To do so, set gridx or gridy to the constant GridBagConstraints.RELATIVE. Unfortunately, it's not as simple as this. Here are a couple of warnings:

  • To place a component to the right of the previous one, set gridx to RELATIVE, AND use the same value for gridy as you used for the previous component.
  • Similarly, to place a component below the previous one, set gridy to RELATIVE, AND leave gridx unchanged.
  • Setting both gridx and gridy to RELATIVE places all the components in one row, not in a diagonal line, as you would expect. (This is the default.)

In other words, if gridx or gridy is RELATIVE, you had better leave the other value unchanged. In short, RELATIVE makes it easy to arrange a lot of components in a row or a column. That's what it was intended for; if you try to do something else, you're fighting against the layout manager, not working with it.

GridBagLayout allows another kind of relative positioning, in which you specify where, in a row or a column, the component should be placed. To do so, you use the gridwidth and gridheight fields of GridBagConstraints. Setting either of these to the constant REMAINDER says that the component should be the last item in its row or column, and therefore should occupy all the remaining space. Setting either gridwidth or gridheight to RELATIVE says that it should be the second to the last item in its row or column. Obviously, you can use these constants to create constraints that can't possibly be met; for example, you can say that two components must be the last component in a row. In these cases, the layout manager tries to do something reasonable--but it will almost certainly do something you don't want. Again, relative placement works well as long as you don't try to twist it into doing something it wasn't designed for.

Composite layouts

Sometimes things don't fall neatly into little boxes. This is true of layouts as well as life. For example, if you want to use some of GridBagLayout's weighting features for part of your GUI, you could create separate layouts for different parts of the GUI, and combine them with yet another layout. That's how we'll build the pocket calculator interface below. We will use three grid bag layouts: one for the first row of buttons (C, %, +), one for the last (0, ., =), and one for the applet itself. The master layout (the applet's) manages the text field we use for the display, the panels containing the first and last rows of buttons, and the twelve buttons in the middle.[2]

[

Here's the code for the Calculator applet. It only implements the user interface (i.e., the keyboard); it collects everything you type in the display field, until you press C (clear). Figuring out how to connect the GUI to some other code that would perform the operations is up to you. One strategy would be to send an event to the object that does the computation whenever the user presses the equals sign. That object could read the contents of the text field, parse it, do the computation, and display the results.

import java.awt.*;
import java.awt.event.*;
public class Calculator extends java.applet.Applet 
                            implements ContainerListener, ActionListener {
    GridBagConstraints gbc = new GridBagConstraints(); {
        gbc.weightx = 1.0;  gbc.weighty = 1.0;
        gbc.fill = GridBagConstraints.BOTH;
    }
    TextField theDisplay = new TextField();
    public void init() {
        setFont( new Font("Monospaced", Font.BOLD, 24) );
        addContainerListener( this );
        gbc.gridwidth=4;
        addGB( this, theDisplay, 0, 0 );
        // make the top row
        Panel topRow = new Panel(); 
        topRow.addContainerListener( this );
        gbc.gridwidth = 1;
        gbc.weightx = 1.0;
        addGB( topRow, new Button("C"), 0, 0 );
        gbc.weightx = 0.33;
        addGB( topRow, new Button("%"), 1, 0 );
        gbc.weightx = 1.0;
        addGB( topRow, new Button("+"), 2, 0 );
        gbc.gridwidth = 4;
        addGB( this, topRow, 0, 1 );
        gbc.weightx = 1.0;  gbc.gridwidth = 1;
        // make the digits
        for(int j=0; j<3; j++)
            for(int i=0; i<3; i++)
                addGB( this, new Button( "" + ((2-j)*3+i+1) ), i, j+2 );
        // -, x, and divide
        addGB( this, new Button("-"), 3, 2 );
        addGB( this, new Button("x"), 3, 3 );
        addGB( this, new Button("\u00F7"), 3, 4 );
        // make the bottom row
        Panel bottomRow = new Panel(); 
        bottomRow.addContainerListener( this );
        gbc.weightx = 1.0;
        addGB( bottomRow, new Button("0"), 0, 0 );
        gbc.weightx = 0.33;
        addGB( bottomRow, new Button("."), 1, 0 );
        gbc.weightx = 1.0;
        addGB( bottomRow, new Button("="), 2, 0 );
        gbc.gridwidth = 4;
        addGB( this, bottomRow, 0, 5 );
    }
    private void addGB( Container cont, Component comp, int x, int y  ) {
        if ( ! (cont.getLayout() instanceof GridBagLayout) )
            cont.setLayout( new GridBagLayout() );
        gbc.gridx = x;  gbc.gridy = y;
        cont.add( comp, gbc );
    }
    public void componentAdded( ContainerEvent e ) {
        Component comp = e.getChild();
        if ( comp instanceof Button )
            ((Button)comp).addActionListener( this );
    }
    public void componentRemoved( ContainerEvent e ) { }
    public void actionPerformed( ActionEvent e ) {
        if ( e.getActionCommand().equals("C") )
            theDisplay.setText( "" );
        else 
            theDisplay.setText( theDisplay.getText() + e.getActionCommand() );
    }
}

Once again, we use an addGB() helper method to add components with their constraints to the layout. Before discussing how to build the display, let's look at addGB(). We said earlier that there are three layout managers in our user interface: one for the applet itself, one for the panel containing the first row of buttons (topRow), and one for the panel containing the bottom row of buttons (bottomRow). We use addGB() for all three layouts; its first argument specifies the container to add the component to. Thus, when the first argument is this, we're adding an object to the applet itself. When the first argument is topRow, we're adding a button to the first row of buttons. addGB() first checks the container's layout manager, and sets it to GridBagLayout if it isn't already set properly. It then sets the object's position by modifying a set of constraints, gbc, and then uses these constraints to add the object to the container.

We use a single set of constraints throughout the applet, modifying fields as we see fit. The constraints are created and initialized at the beginning of the applet, using a nonstatic initializer block. Before calling addGB(), we set any fields of gbc for which the defaults are inappropriate. Thus, for the display itself, we set the grid width to 4, and add the display directly to the applet (this). The add() method, which is called by addGB(), makes a copy of the constraints, so we're free to reuse gbc throughout the applet.

The first row of buttons (and the last) are the motivation for the composite layout. Using a single GridBagLayout, it's very difficult (or impossible) to create buttons that aren't aligned with the grid; that is, you can't say "I want the C button to have a width of 1.5." Therefore, topRow has its own layout manager, with three horizontal cells, allowing each button in the row to have a width of 1. To control the size of the buttons, we set the weightx variables so that the "clear" and "plus" buttons take up more space than the "percent" button. We then add the topRow as a whole to the applet, with a width of 4. The bottom row is built similarly.

To build the buttons for the digits 1-9, we use a doubly nested loop. There's nothing particularly interesting about this loop, except that it's probably a bit too clever for good taste. The minus, multiply, and divide buttons are also simple: we create a button with the appropriate label, and use addGB() to place it in the applet. It's worth noting that we used a Unicode constant to request a real division sign, rather than wimping out and using a slash.

That's it for the user interface; the only topic left is event handling. Each button generates action events; we need to register listeners for these events. We'll make the applet the listener for all the buttons. To register the applet as a listener, we'll be clever. Whenever a component is added to a container, the container generates a ContainerEvent. Therefore, we can write componentAdded() and componentRemoved() methods, declare that the applet is a ContainerListener, and use componentAdded() to register listeners for our buttons. This means that the applet must register as a ContainerListener for itself, and for the two panels, topRow and bottomRow. Our componentAdded() method is very simple. It calls getChild() to find out what component caused the event (i.e., what component was added). If that component is a button, it registers the applet as an ActionListener for that button.

actionPerformed() is called whenever the user presses any button. It clears the display if the user pressed the "C" button; otherwise, it appends the button's action command (in this case, its label) to the display.

Combining layout managers is an extremely useful trick. Granted, this applet verges on overkill. You won't often need to create a composite layout using multiple grid bags. Composite layouts are most common with the BorderLayout; you'll frequently use different layout managers for each of a border layout's regions. For example, the Center region might be a ScrollPane, which has its own special-purpose layout manager; the East and South regions might be panels managed by grid layouts or flow layouts, as appropriate.

12.6 Nonstandard Layout Managers

We've covered the basic layout managers; with them, you should be able to create just about any user interface you like.

But that's not all, folks. If you want to experiment with layout managers that are undocumented, may change, and may not be available locally on all platforms, look in the sun.awt classes. You'll find a HorizBagLayout, a VerticalBagLayout, an OrientableFlowLayout, and a VariableGridLayout. Furthermore, public-domain layout managers of all descriptions are beginning to appear on the Net; keep your eye on Gamelan and the other Java archives.

12.7 Absolute Positioning?

It's possible to set the layout manager to null: no layout control. You might do this to position an object on the display at some absolute coordinates. This is almost never the right approach. Components might have different minimum sizes on different platforms, and your interface would not be very portable.

The following applet doesn't use a layout manager and works with absolute coordinates instead:

import java.awt.*; 
 
public class MoveButton extends java.applet.Applet {  
    Button button = new Button("I Move"); 
 
    public void init() { 
        setLayout( null ); 
        add( button ); 
        button.setSize( button.getPreferredSize() ); 
        button.move( 20, 20); 
    } 
 
    public boolean mouseDown( Event e, int x, int y ) { 
        button.move( x, y ); 
        return ( true ); 
    } 
 
} 

Click in the applet area, outside of the button, to move the button to a new location. If you are running the example in an external viewer, try resizing the window and note that the button stays at a fixed position relative to the display origin.

to previous section to next section