Java AWT | GridBagLayout Class – GeeksforGeeks

GridBagLayout class is a flexible layout manager. It is used to aligns the components horizontally, vertically, or along their baseline. It doesn’t require the components of the same size. Each GridBagLayout object manages a rectangular grid of cells, dynamic with each component occupying one or more cells, called its display area. GridBagLayout components are associated with the instance of GridBagConstraints. These constraints are used to define the component’s display area and their positions. In addition to its constraints object, the GridBagLayout also considers each component’s minimum and preferred sizes in order to determine a component’s size. GridBagLayout components are also arranged in the rectangular grid but can have different sizes and can occupy the multiple rows or columns.

Constructor: 

  • GridBagLayout(): It is used to creates a grid bag layout manager.

Commonly Used Methods:  

  • removeLayoutComponent(Component cmp): Removes the specified component from this layout.
  • getLayoutAlignmentY(Container p): Returns the alignment along the y-axis.
  • addLayoutComponent(Component cmp, Object cons): Add the specified component with the specified name to the layout.
  • toString(): Returns a string representation of this grid bag layout’s values.
  • getLayoutAlignmentX(Container p): Returns the alignment along the x-axis.
  • getConstraints(Component cmp): Gets the constraints for the specified component.
  • maximumLayoutSize(Container tar): Returns the maximum dimensions for this layout given the components in the specified target container.
  • minimumLayoutSize(Container par): Determines the minimum size of the parent container using this grid bag layout.

Below programs illustrate the GridBagLayout class:  

  • Program 1: Below program arranges the several row and column components in a JFrame, whose instance class is named as “Gridbagdemo”. We create 4 JButton components named “java“, “layout“, “manager“, “demo” and then add them to the JFrame by the method add(). We set the size and visibility of the frame by method setSize() and setVisible(). The layout is set by the method setLayout().

Tóm Tắt

Java




import java.awt.*;

import java.awt.event.*;

import javax.swing.JFrame;

import javax.swing.*;

 

public class GridbagDemo extends JFrame {

 

    GridbagDemo()

    {

 

        

        setTitle("GridBagLayoutDemo");

 

        

        JPanel p = new JPanel();

 

        

        p.setLayout(new GridBagLayout());

 

        

        GridBagConstraints c = new GridBagConstraints();

 

        

        c.insets = new Insets(2, 2, 2, 2);

 

        

        c.gridx = 0;

 

        

        c.gridy = 0;

 

        

        c.ipadx = 15;

 

        

        c.ipady = 50;

 

        

        p.add(new JButton("Java Swing"), c);

 

        

        c.gridx = 1;

 

        

        c.ipadx = 90;

 

        

        c.ipady = 40;

 

        

        p.add(new JButton("Layout"), c);

 

        

        c.gridx = 0;

 

        

        c.gridy = 1;

 

        

        c.ipadx = 20;

 

        

        c.ipady = 20;

 

        

        p.add(new JButton("Manager"), c);

 

        

        c.ipadx = 10;

 

        

        c.gridx = 1;

 

        

        p.add(new JButton("Demo"), c);

 

        

        

        WindowListener wndCloser = new WindowAdapter() {

 

            public void windowClosing(WindowEvent e)

            {

 

                

                System.exit(0);

            }

        };

 

        

        addWindowListener(wndCloser);

 

        

        getContentPane().add(p);

 

        

        setSize(600, 400);

 

        

        setVisible(true);

    }

 

    

    public static void main(String[] args)

    {

 

        

        new GridbagDemo();

    }

}



Output:

  • Program 2: Below program arranges the several row and column components in a JFrame, whose instance class is named as “Gridbagdemo”. We create 5 JButton components and then add them to the JFrame by the method add(). We set the title, size and visibility of the frame by method setTitle, setSize() and setVisible(). The layout is set by the method setLayout().

Java




import java.awt.*;

import javax.swing.JButton;

import javax.swing.JFrame;

  

public class GridBagLayoutDemo {

  

    final static boolean shouldFill = true;

    final static boolean shouldWeightX = true;

    final static boolean RIGHT_TO_LEFT = false;

  

public static void addComponentsToPane(Container pane)

{

 

    

    if (RIGHT_TO_LEFT) {

 

        pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    }

 

    

    JButton button;

 

    

    pane.setLayout(new GridBagLayout());

 

    

    GridBagConstraints c = new GridBagConstraints();

 

    

    if (shouldFill) {

 

        

        c.fill = GridBagConstraints.HORIZONTAL;

    }

 

    

    

    button = new JButton("Button 1");

 

    

    if (shouldWeightX) {

        c.weightx = 0.5;

    }

 

    

    c.gridx = 0;

 

    

    c.gridy = 0;

 

    

    pane.add(button, c);

 

    

    

    button = new JButton("Button 2");

 

    

    c.gridx = 1;

 

    

    c.gridy = 0;

 

    

    pane.add(button, c);

 

    

    

    button = new JButton("Button 3");

 

    

    c.gridx = 2;

 

    

    c.gridy = 0;

 

    

    pane.add(button, c);

 

    

    

    button = new JButton("Long-Named Button 4");

 

    

    c.ipady = 40;

 

    

    c.weightx = 0.0;

 

    

    c.gridwidth = 3;

 

    

    c.gridx = 0;

 

    

    c.gridy = 1;

 

    

    pane.add(button, c);

 

    

    

    button = new JButton("Button 5");

 

    

    c.ipady = 0;

 

    

    c.weighty = 1.0;

 

    

    c.anchor = GridBagConstraints.PAGE_END;

 

    

    c.insets = new Insets(10, 0, 0, 0);

 

    

    c.gridx = 1;

 

    

    c.gridwidth = 2;

 

    

    c.gridy = 2;

 

    

    pane.add(button, c);

}

 

private static void createAndShowGUI()

{

 

    

    JFrame.setDefaultLookAndFeelDecorated(true);

 

    

    JFrame frame = new JFrame("GridBagLayoutDemo");

 

    

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

 

    

    addComponentsToPane(frame.getContentPane());

 

    

    frame.pack();

 

    

    frame.setVisible(true);

}

 

    

    public static void main(String[] args)

    {

  

        

        

        javax.swing.SwingUtilities.invokeLater(new Runnable() {

  

            public void run()

            {

                createAndShowGUI();

            }

        });

    }

}



Output:

Note: The above programs might not run in an online IDE. Please use an offline compiler.
Reference: https://docs.oracle.com/javase/7/docs/api/java/awt/GridBagLayout.html
 

My Personal Notes

arrow_drop_up