Thursday 14 May 2015

GridBagLayout in Java

  This is the most powerful, complex, and hard-to-use Layout Manager that comes with the AWT. Although it gives you the most flexibility, you should plan to spend some time experimenting with its parameters before you get a layout that you like. The basic principle of GridBagLayout is that you associate a constraint object, an instance of GridBagConstraints, with each component in the layout. The GridBagLayout Manager uses those constraints to determine how to lay out the components on an invisible grid, where each component can occupy one or more grid cells. The creator methods for GridBagConstraints take no input parameters; you customize the instance by changing the following instance variables.
anchor
Specifies how a component is to be aligned if a component is smaller than the allocated space. The available constants follow:
CENTER: Puts the component in the middle of the area.
EAST: Aligns it with the right-middle side.
NORTH: Aligns it with the top-middle.
NORTHEAST: Puts it in the upper-right corner.
NORTHWEST: Puts it in the upper-left corner.
SOUTH: Aligns it with the bottom-middle.
SOUTHEAST: Puts it in the lower-right corner.
SOUTHWEST: Puts it in the lower-left corner.
WEST: Aligns it with the left-middle side.
fill
Determines what happens if the space allotted to a component is larger than its default size. The allowable values follow: BOTH: Tells the component to fill the space in both directions.
HORIZONTAL: Tells the component to fill the space in the horizontal direction.
NONE: Leaves the component at its default size.
VERTICAL: Tells the component to fill the space in the vertical direction.

gridheight
Specifies the height of the component in grid cells. The constant REMAINDER specifies that the component is the last one in the column and therefore should get all the remaining cells.
gridwidth
Specifies the width of the component in grid cells. The constant REMAINDER specifies that the component is the last one in the row and therefore should get all the cells remaining in the row.
gridx
Specifies the grid position of the left side of a component in the horizontal direction. The constant RELATIVE specifies the position to the right of the previous component.
gridy
Specifies the grid position of the top of a component in the vertical direction. The constant RELATIVE specifies the position below the previous component.
insets
Enables you to set an instance of the Insets class that specifies the whitespace reserved around an object. It provides more flexibility than ipadx and ipady because it allows different whitespace on the left than on the right and different whitespace on the top than on the bottom of the component.
ipadx
Specifies the amount of padding (empty space) to put on either side of a component. This increases the effective size of the component.
ipady
Specifies the amount of padding to put above and below the component.
weightx
Specifies how extra horizontal space (space not needed for the default component sizes) is allocated between components. This is a relative value, normally chosen to be between 0 and 1, and the values of the components are compared when allocating space. If one component has a weight of .7 and another has a weight of .2, for example, the one with weight .7 gets more of the extra space than the one with .2.
weighty
Same as weightx but for the vertical direction.
As you play around with this Layout Manager, you'll discover that it's very powerful but not entirely intuitive. Listing 17.29 shows an example that gives you a feeling for what GridBagLayout can do.

Listing 17.29. Using GridBagLayout.

import java.awt.*;
import java.applet.Applet;


public class gridbaglayout extends Applet{
    Button the_buttons[];

    public void init()
    {   int i;
        int n_buttons;
        GridBagLayout gbl;
        GridBagConstraints gbc;
        String name;
        int j;
        //define a new GridBagLayout
        gbl = new GridBagLayout();
        //define a new GridBagConstraints. this will be used
        //for all the components
        gbc = new GridBagConstraints();
        //if a component gets more space than it needs don't grow
        //the component to fit the
        //available space
        gbc.fill = GridBagConstraints.NONE;
        //if a component doesn't fill the space assigned to it
        //put it in the top middle of the area
        gbc.anchor = GridBagConstraints.NORTH;
        //pad the size of the component
        gbc.ipadx = 5;
        gbc.ipady = 5;
        //if there is more width available than needed for
        //the components give this
        //component a weight of .3 when allocating the extra horizontal space
        gbc.weightx = .3;
        gbc.weighty = .1;
        setLayout(gbl);
        n_buttons = 15;
        the_buttons = new Button[n_buttons];
        j = 0;
        for(i=0;i<9;i++) {
            j++;
            //start a new row after every 3 buttons
            if (j == 3) {
                j = 0;
                //make this component the last one in the row
                gbc.gridwidth = GridBagConstraints.REMAINDER;
            }
            name = "Button " + i;
            the_buttons[i] = new Button(name);
            //tell GridBagLayout which constraint object to use. you can use
            //the same constraint object for many components even if you
            //change the instance variables values in the constraints object
            //for different components
            gbl.setConstraints(the_buttons[i],gbc);
            add(the_buttons[i]);
            //this sets the gridwidth to its default value. it cleans up the
            //REMAINDER value assigned to the last button in a row
            gbc.gridwidth = 1;
        }
        //change the weight for allocating extra space to subsequent
        gbc.weightx = .4;
        gbc.weighty = .2;
        name = "Button 9";
        the_buttons[9] = new Button(name);
        //if there's extra space put the component in the upper right corner
        gbc.anchor = GridBagConstraints.NORTHEAST;
        //make it the last component in the row
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbl.setConstraints(the_buttons[9],gbc);
        add(the_buttons[9]);
        name = "Button 10";
        the_buttons[10] = new Button(name);
        //if the component has extra space assigned grow the component to
        //fill it in both the x and y direction
        gbc.fill = GridBagConstraints.BOTH;
        //this line is unnecessary because the value of gridwidth is retained
        //from when it was set for the previous button
        gbc.gridwidth = GridBagConstraints.REMAINDER;
        gbl.setConstraints(the_buttons[10],gbc);
        add(the_buttons[10]);
        name = "Button 11";
        the_buttons[11] = new Button(name);
        //if there's extra space align the component with the right hand side
        gbc.anchor = GridBagConstraints.EAST;
        //change the weights for allocating extra space
        gbc.weightx = .5;
        gbc.weighty = 0;
        //don't grow the component in either direction if there's extra space
        gbc.fill = GridBagConstraints.NONE;
        gbl.setConstraints(the_buttons[11],gbc);
        add(the_buttons[11]);
        name = "Button 12";
        the_buttons[12] = new Button(name);
        //pad the component--on the Mac the component grows
        gbc.ipadx = 20;
        //set the allocation of width to the default.
        //note that it had been set to
        //REMAINDER above
        gbc.gridwidth = 1;
        //put this component to the right and below the previous one
        gbc.gridx = GridBagConstraints.RELATIVE;
        gbc.gridy = GridBagConstraints.RELATIVE;
        gbl.setConstraints(the_buttons[12],gbc);
        add(the_buttons[12]);
        name = "Button 13";
        the_buttons[13] = new Button(name);
        //set the pad space to 0
        gbc.ipadx = 0;
        //align the component with the left hand side of the available space
        gbc.anchor = GridBagConstraints.WEST;
        gbl.setConstraints(the_buttons[13],gbc);
        add(the_buttons[13]);
    }


}

No comments:

Post a Comment