The Java Developers Almanac 1.4


Order this book from Amazon.

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

e622. Creating a GridBagLayout

A gridbag layout is the most sophisticated layout manager in Java's UI toolkit. This example demonstrates how to create and set a gridbag layout and how to set gridbag constraints on components added to the layout. See other examples for code that demonstrates the usage of available gridbag constraints.
    JFrame frame = new JFrame();
    Container container = frame.getContentPane();
    
    // Create the layout
    GridBagLayout gbl = new GridBagLayout();
    
    // Set layout on container
    container.setLayout(gbl);
    
    // Place a component at cell location (1,1)
    GridBagConstraints gbc = new GridBagConstraints();
    gbc.gridx = 1;
    gbc.gridy = 1;
    // Add other gridbag constraints here
    
    // Associate the gridbag constraints with the component
    gbl.setConstraints(component, gbc);
    
    // Add the component to the container
    container.add(component);
    
    // Show the frame
    frame.pack();
    frame.setVisible(true);

 Related Examples
e623. Setting the Location of a Component in a GridBagLayout
e624. Getting the Number of Rows and Columns of Cells in a GridBagLayout
e625. Making a GridBagLayout Fill the Container
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.