The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > java.awt  [78 examples] > GridBagLayout  [12 examples]

e625. Making a GridBagLayout Fill the Container

By default, a gridbag layout arranges its components in the smallest area that satisfies the preferred sizes of the components. If the container is larger than this area, the cluster of components is centered within the container. This example demonstrates how to spread the excess space among all the cells in the gridbag layout.

It is possible to control exactly how much excess space is distributed among the gridbag layout cells. See e626 Setting the Stretchyness of Rows and Columns in a GridBagLayout Using Layout Weights for more details.

    GridBagLayout gbl = new GridBagLayout();
    container.setLayout(gbl);
    
    // Add components to container and gbl
    
    // Force the layout of components before calling getLayoutWeights()
    // otherwise the result of getLayoutWeights() is not valid
    gbl.layoutContainer(container);
    
    // Set weights of all columns and rows to 1
    double[][] weights = gbl.getLayoutWeights();
    for (int i=0; i<2; i++) {
        for (int j=0; j<weights[i].length; j++) {
            weights[i][j] = 1;
        }
    }
    gbl.columnWeights = weights[0];
    gbl.rowWeights = weights[1];

 Related Examples
e622. Creating a GridBagLayout
e623. Setting the Location of a Component in a GridBagLayout
e624. Getting the Number of Rows and Columns of Cells in a GridBagLayout
e626. Setting the Stretchyness of Rows and Columns in a GridBagLayout Using Layout Weights
e627. Setting the Stretchyness of Columns and Rows in a GridBagLayout Using Component Weights
e628. Setting the Stretchyness of a Component Within the Cell of a GridBagLayout Using Fill
e629. Setting the Location of a Component Within the Cell of a GridBagLayout Using Anchors
e630. Setting the Space around a Component Within the Cell of the GridBagLayout Using Insets
e631. Adjusting the Size of a Component in a GridBadLayout Using Internal Padding
e632. Setting a Row or Column of a GridBadLayout to a Particular Size
e633. Setting Gap Sizes in a GridBadLayout

See also: Colors    Components    Containers    Cursors    Drawing    Events    Focus    Frames    Images    Shapes    Simulating Events    Text    The Screen   


© 2002 Addison-Wesley.