GridBag Layout in Java Swing

GridBag Layout is complex when compared to the other layouts provided in the AWT. GridBag Layout allows the user to organize components in multiple rows and columns, stretch specific rows and columns when space is available, and anchor objects in different corners.

 

import java.awt.*;

import javax.swing.*;

public class gridBag extends JApplet

{

JPanel panelObj;

GridBagLayout gbObj;

GridBagConstraints gbCons;

public void init()

{

gbObj=new GridBagLayout();

gbCons=new GridBagConstraints();

panelObj=new JPanel();

getContentPane().add(panelObj);

panelObj.setLayout(gbObj);

JButton buttonaa=new JButton("Button aa");

JButton buttonbb=new JButton("Button bb");

JButton buttoncc=new JButton("Button cc");

JButton buttondd=new JButton("Button dd");

gbCons.fill=GridBagConstraints.BOTH;

gbCons.anchor=GridBagConstraints.CENTER;

gbCons.gridwidth=1;

gbCons.weightx=1.0;

gbObj.setConstraints(buttonaa,gbCons);

panelObj.add(buttonaa);                                                                          

gbCons.gridwidth=GridBagConstraints.REMAINDER;

gbObj.setConstraints(buttonbb,gbCons);

panelObj.add(buttonbb);

gbCons.gridwidth=GridBagConstraints.REMAINDER;

gbObj.setConstraints(buttoncc,gbCons);

panelObj.add(buttoncc);

gbCons.fill=GridBagConstraints.BOTH;

gbCons.anchor=GridBagConstraints.CENTER;

gbCons.gridwidth=1;

gbCons.weightx=1.0;

gbObj.setConstraints(buttondd,gbCons);

panelObj.add(buttondd);

}

}

//<applet code="gridBag.class" height=500 width=500></applet>

GridLayout in Java

In this example,

1 First,java.awt.* and javax.swing.*packages and imported.

2 Then,a class gridBag is created,which extends JApplet class.

3 in the class gridBag,

  • A JPanel object panelObj is declared
  • The GridBagLayout object gbobj is declared.
  • The gridbagconstraints are then declared by using gbCons.
  • The init() method is then called.
  • In the init() method,
    1. A new instance of the GridBag layout manager is created and assigned to gbObj.
    2. A new instance of the GridBagConstraints layout manager is created and assigned to gbCons.
    3. Then, panelObj=(JPanel)getContentPane(); statement is used to get the contentpane value and assign it to panelObj.
    4. The setLayout() method is called to set the layout gbObj for the panel panelObj.
    5. Then, new instances of JButtons are created with values Buttonaa, Buttonbb, Buttoncc and Buttondd. These values are assigned to buttonaa,buttonbb,buttoncc and buttondd respectively.
    6. The fill attribute is used when a component is smaller than its display area for ascertaining whether the component needs to be stretched within its display area. The GridBagConstraints.BOTH fills the display area completely.
    7. The anchor attribute is used when a component is smaller than its display area. GridBagConstraints.CENTER is the default value.
    8. The gridwidth and weightx of gbCons are assigned values of 1 and 1.0, respectively. The gridwidth attribute specifies the component. The attribute weightx specifies area of the component. The attribute weightx specifies whether the components stretch horizontally to fill the display area of the applet.
    9. The setConstraints() of the GridBagLayout class is used for associating the constraints with the component.
    10. Then, the buttonaa is added to the panelObj using the add().
    11. The remainder value of the GridBagConstraints is assigned to the gridwidth of gbCons.
    12. Then, setConstraints() is called to set the constraints buttonbb and gbCons of the object gbObj.
    13. The buttonbb is then added to panelObj using the add().
    14. The remainder value of GridBagConstraints is assigned to the gridwidth of gbCons.
    15. Then, setConstraints() is called to set the constraints buttoncc and gbCons, of the object gbObj.
    16. The buttoncc is then added to panelObj using the add().
    17. The GridBagConstraints.BOTH fills the display area completely and assign the result to gbCons.fill.
    18. Then GridBagConstraints.CENTER anchor attribute is used as the default value of gbCons.
    19. The gridwidth and weightx of gbCons are assigned the value of1 abd 1.0,respectively.
    20. Then, setConstraints() is called to set the constraints buttondd and gbCons of the object gbObj.
    21. Finally, the buttondd is then added to panelObj using the add().