• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » Java » Swing » GridBagLayout in Java Swing Example
Next →
← Prev

GridBagLayout in Java Swing Example

By Dinesh Thakur

The grid bag layout manager is the most advanced and yet easy to use layout manager. A GridBagLayout arranges the component in a grid of rows and columns. It allows different sized components to span multiple rows or columns. Also, each row in the grid can have different number of columns. Grid bag layout specifies a grid of cells with the container determines the component’s size and then positions each component in one or more cell accordingly.

The grid bag layout manager can be created by using the following constructor.

GridBagLayout ()

Information such as, size and location of each component in a grid bag is determined by a set of constraints contained in an object of type GridBagConstraints. The variables in the class GridBagConstrain ts that represent these constraints are given below:

• gridx and gridy: gridx and gridy specify the x and y coordinates to position the component. gridx and grid y represent the number of cells at the left of the component and the number of cells at the top of the component, respectively. By default, the value of both gridx and gridy is relative. It places the component at position next to the component added last in a row or a column.

• gridwidth and gridheight: gridwidth and gridheight specify the number of columns and rows, respectively occupied by the components. The default value for both gridwidth and gridheight is 1. The value REMAINDER can be assigned to gridwidth and gridheight to indicate that the component should be the last one in a row or a column. Ifwe want the component to be the next to last one in a row or a column, the value RELATIVE is used.

• fill: fill specifies how the component should expand within its display area if the area is larger than the component. Any one of the following values can be assigned to fill.

NONE: To specify that the size of the component remains unchanged. It is the default value.

VERTICAL: To specify that the component can expand only vertically.

HORIZONTAL: To specify that the component can expand only horizontally.

BOTH: To specify that the component can expand vertically as well as horizontally.

• ipadx and ipady: ipadx and ipady specify extra width and height of the component, respectively. The default value is 0. Negative values can also be used to tighten the spacing between the components.

• insets: It specifies the amount of space to leave between the borders of the component and the edges of display area. The Insets class specifies the separate values of top, left, bottom, and right. The default value is (0,0,0,0).

• anchor : It specifies where the component should be placed within the display area if it is smaller than its display area. The location of the component is usually given as a compass direction. Any one of the following values can be assigned to anchor.

CENTER(default), NORTH, SOUTH, NORTHEAST, SOUTHWEST, EAST, WEST, SOUTHEAST,NORTHWEST

• weightx and weighty: weightx and weighty specify how the extra horizontal and vertical spaces be adjusted while resizing the container. The component will occupy extra space in the specific direction according to the weight assigned to it.

Consider Example. In this example, grid bag layout is set as the layout for the frame. Five buttons are created and their grid bag constraints are specified. Then the buttons are added to the frame.

Example: A Program to demonstrate the use of GridBaglayout

import java.awt.*;

import javax.swing.*;

class myGridBag extends JApplet

{

    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 gb =new GridBagConstraints();

               button= new JButton(“Button 1”);

               if (shouldWeightX)

                  {

                     gb.weightx = 0.5;

                  }

               gb.fill = GridBagConstraints.HORIZONTAL;

               gb.gridx = 0;

               gb.gridy = 0;

               pane.add(button, gb);

               button= new JButton(“Button 2”);

               gb.fill = GridBagConstraints.HORIZONTAL;

               gb.weightx = 1;

               gb.gridx = 1;

               gb.gridwidth = 2;

               gb.gridy = 0;

               pane.add(button, gb);

               button= new JButton(“Button 3”);

               gb.fill = GridBagConstraints.VERTICAL;

               gb.ipady = 30;

               gb.weightx = 2;

               gb.gridwidth = 4;

               gb.gridx = 0;

               gb.gridy = 1;

               pane.add(button, gb);

               button= new JButton(“Button 4”);

               gb.fill = GridBagConstraints.VERTICAL;

               gb.ipady = 0; //reset to default

               gb.weighty = 1.0;

               gb.anchor = GridBagConstraints.PAGE_END;

               //bottom of space

               gb.insets =new Insets(10,0,0,0); //top padding

               gb.gridx = 1;

               gb.gridwidth = 2;

               gb.gridy = 2; //third row

               pane.add(button, gb);

               button= new JButton(“Button 5”);

               gb.fill = GridBagConstraints.HORIZONTAL;

               gb.weighty = 1.0;

               gb.anchor = GridBagConstraints.PAGE_END;

               gb.insets =new Insets(10,0,5,0);

               gb.gridx = 0;

               gb.gridwidth = 2;

               gb.gridy = 3; //fourth row

               pane.add(button, gb);

     }

              private static void displayGUI()

              {

                  JFrame frame= new JFrame(“GridBagLayoutDemo”);

                  frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

                  addComponentsToPane(frame.getContentPane());

                  frame.setSize(500, 300);

                  frame.setVisible(true);

              }

                public static void main(String[] args)

                {

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

                     {

                        public void run()

                        {

                           displayGUI ();

                        }

                     }) ;

                }

}

The output of the program is shown in Figure

               GridBagLayout in Java Swing Example

You’ll also like:

  1. GridBagLayout in Java Example
  2. JList in Java Swing Example
  3. SpringLayout in Java Swing Example
  4. FocusListener in Java Swing Example
  5. CheckerBoard Java Swing Example
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

SQL Tutorials

SQL Tutorials

  • SQL - Home
  • SQL - Select
  • SQL - Create
  • SQL - View
  • SQL - Sub Queries
  • SQL - Update
  • SQL - Delete
  • SQL - Order By
  • SQL - Select Distinct
  • SQL - Group By
  • SQL - Where Clause
  • SQL - Select Into
  • SQL - Insert Into
  • SQL - Sequence
  • SQL - Constraints
  • SQL - Alter
  • SQL - Date
  • SQL - Foreign Key
  • SQL - Like Operator
  • SQL - CHECK Constraint
  • SQL - Exists Operator
  • SQL - Drop Table
  • SQL - Alias Syntax
  • SQL - Primary Key
  • SQL - Not Null
  • SQL - Union Operator
  • SQL - Unique Constraint
  • SQL - Between Operator
  • SQL - Having Clause
  • SQL - Isnull() Function
  • SQL - IN Operator
  • SQL - Default Constraint
  • SQL - Minus Operator
  • SQL - Intersect Operator
  • SQL - Triggers
  • SQL - Cursors

Advanced SQL

  • SQL - Joins
  • SQL - Index
  • SQL - Self Join
  • SQL - Outer Join
  • SQL - Join Types
  • SQL - Cross Join
  • SQL - Left Outer Join
  • SQL - Right Join
  • SQL - Drop Index
  • SQL - Inner Join
  • SQL - Datediff() Function
  • SQL - NVL Function
  • SQL - Decode Function
  • SQL - Datepart() Function
  • SQL - Count Function
  • SQL - Getdate() Function
  • SQL - Cast() Function
  • SQL - Round() Function

Other Links

  • SQL - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW