Java AWT | GridBagLayout Class Last Updated : 17 Aug, 2021 Comments Improve Suggest changes Like Article Like Report 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(). Java // Java program to demonstrate GridBagLayout class. import java.awt.*; import java.awt.event.*; import javax.swing.JFrame; import javax.swing.*; // class extends JFrame public class GridbagDemo extends JFrame { GridbagDemo() { // Function to set title of JFrame. setTitle("GridBagLayoutDemo"); // Creating Object of Jpanel class JPanel p = new JPanel(); // set the layout p.setLayout(new GridBagLayout()); // creates a constraints object GridBagConstraints c = new GridBagConstraints(); // insets for all components c.insets = new Insets(2, 2, 2, 2); // column 0 c.gridx = 0; // row 0 c.gridy = 0; // increases components width by 10 pixels c.ipadx = 15; // increases components height by 50 pixels c.ipady = 50; // constraints passed in p.add(new JButton("Java Swing"), c); // column 1 c.gridx = 1; // increases components width by 70 pixels c.ipadx = 90; // increases components height by 40 pixels c.ipady = 40; // constraints passed in p.add(new JButton("Layout"), c); // column 0 c.gridx = 0; // row 2 c.gridy = 1; // increases components width by 20 pixels c.ipadx = 20; // increases components height by 20 pixels c.ipady = 20; // constraints passed in p.add(new JButton("Manager"), c); // increases components width by 10 pixels c.ipadx = 10; // column 1 c.gridx = 1; // constraints passed in p.add(new JButton("Demo"), c); // Creating Object of "wndcloser" // class of windowlistener WindowListener wndCloser = new WindowAdapter() { public void windowClosing(WindowEvent e) { // exit the system System.exit(0); } }; // add the actionwindowlistener addWindowListener(wndCloser); // add the content getContentPane().add(p); // Function to set size of JFrame. setSize(600, 400); // Function to set visibility of JFrame. setVisible(true); } // Main Method public static void main(String[] args) { // calling the constructor 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 // Java program to demonstrate GridBagLayout class. import java.awt.*; import javax.swing.JButton; import javax.swing.JFrame; // Constructor of GridBagLayout class. 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 condition if (RIGHT_TO_LEFT) { pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT); } // Declaration of objects of JButton class JButton button; // set the layout pane.setLayout(new GridBagLayout()); // creates a constraints object GridBagConstraints c = new GridBagConstraints(); // if condition if (shouldFill) { // natural height, maximum width c.fill = GridBagConstraints.HORIZONTAL; } // Initialization of object // "button" of JButton class. button = new JButton("Button 1"); // if condition if (shouldWeightX) { c.weightx = 0.5; } // column 0 c.gridx = 0; // row 0 c.gridy = 0; // Adding JButton "button" on JFrame. pane.add(button, c); // Initialization of object // "button" of JButton class. button = new JButton("Button 2"); // column 1 c.gridx = 1; // row 0 c.gridy = 0; // Adding JButton "button" on JFrame. pane.add(button, c); // Initialization of object // "button" of JButton class. button = new JButton("Button 3"); // column 1 c.gridx = 2; // row 0 c.gridy = 0; // Adding JButton "button" on JFrame. pane.add(button, c); // Initialization of object // "button" of JButton class. button = new JButton("Long-Named Button 4"); // increases components height by 40 pixels c.ipady = 40; // column width 0 c.weightx = 0.0; // row width 3 c.gridwidth = 3; // column 1 c.gridx = 0; // row 1 c.gridy = 1; // Adding JButton "button" on JFrame. pane.add(button, c); // Initialization of object // "button" of JButton class. button = new JButton("Button 5"); // increases components height by 0 pixels c.ipady = 0; // request any extra vertical space c.weighty = 1.0; // bottom of space c.anchor = GridBagConstraints.PAGE_END; // top padding c.insets = new Insets(10, 0, 0, 0); // column 2 c.gridx = 1; // 2 columns wide c.gridwidth = 2; // row 3 c.gridy = 2; // Adding JButton "button" on JFrame. pane.add(button, c); } // Create the GUI and show it. For thread safety, // this method should be invoked from the // event-dispatching thread. private static void createAndShowGUI() { // to set a Jframe default JFrame.setDefaultLookAndFeelDecorated(true); // Create and set up the window. JFrame frame = new JFrame("GridBagLayoutDemo"); // Function to close the operation of JFrame. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // adding the content pane. addComponentsToPane(frame.getContentPane()); // Display the window. frame.pack(); // Function to set visible status of JFrame. frame.setVisible(true); } // Main Method public static void main(String[] args) { // Schedule a job for the event-dispatching thread: // creating and showing this application's GUI. 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 Comment More infoAdvertise with us Next Article Java AWT | GridBagLayout Class S Shivi_Aggarwal Follow Improve Article Tags : Java Java-AWT Practice Tags : Java Similar Reads Java AWT | GridLayout Class GridLayout class represents a layout manager with a specified number of rows and columns in a rectangular grid. The GridLayout container is divided into an equal-sized of rectangles, and one of the components is placed in each rectangle. Every rectangle cell has the same size therefore, they contain 5 min read Java AWT | SpringLayout Class A SpringLayout class in AWT(Abstract Window Toolkit) laid out of the children to its associated container, according to a set of Layout constraints. Each constraint is represented by a Spring object which controls the vertical or horizontal distance between two component edges. The edges can belong 5 min read Java Swing | GroupLayout Class GroupLayout is a LayoutManager that hierarchically group the components and arranges them in a Container. Grouping is done by using the instances of the Group class. It is generally used for developing a GUI ( Graphic User Interface) builders such as Matisse, the GUI builder provided with the NetBea 6 min read Java AWT | FlowLayout FlowLayout is used to arrange components in a sequence one after the other. The default layout of applet and panel is FlowLayout. Constructors : FlowLayout(): It will Construct a new FlowLayout with centered alignment.The horizontal and vertical gap will be 5 pixels. FlowLayout(int align) : It will 5 min read Java AWT | BoxLayout Class The BoxLayout class is used to arrange the components either vertically (along Y-axis) or horizontally (along X-axis). In BoxLayout class, the components are put either in a single row or a single column. The components will not wrap so, for example, a horizontal arrangement of components will stay 5 min read Java AWT | Canvas Class Canvas class is a part of Java AWT. Canvas is a blank rectangular area where the user can draw or trap input from the user. Canvas class inherits the Component class.Constructor of the Canvas class are :Â Â Canvas(): Creates a new blank canvas.Canvas(GraphicsConfiguration c): Creates a new canvas wit 3 min read JavaFX | HBox Class HBox is a part of JavaFX. HBox lays out its children in form of horizontal columns. If the HBox has a border and/or padding set, then the contents will be layed out within those insets. HBox class extends Pane class. Constructors of the class: HBox(): Creates an HBox object with no nodes. HBox(doubl 5 min read Java AWT CheckboxGroup Java AWT CheckboxGroup is a class that is used to create checkboxes and group a set of checkbox buttons together. Many components in the CheckboxGroup separate, which serves as a means to group a set of checkboxes. In this clause, we will delve into the CheckboxGroup class, and its methods, and demo 3 min read JavaFX | FlowPane Class FlowPane class is a part of JavaFX. Flowpane lays out its children in such a way that wraps at the flowpane's boundary. A horizontal flowpane (the default) will layout nodes in rows, wrapping at the flowpane's width. A vertical flowpane lays out nodes in columns, wrapping at the flowpane's height. F 6 min read Component Class in Java The Component class is the superclass of all components. A component class can be linked with a page, components of web applications. Component clearly shows that is the graphical representation of an Object.Important methods of Component Class:public void add(Component c): This method inserts a com 9 min read Like