What is the importance of the GridBagConstraints class in Java?


A GridBagLayout is a very flexible layout manager that allows us to position the components relative to one another using constraints. Each GridBagLayout uses a dynamic rectangular grid of cells with each component occupying one or more cells called its display area. Each component managed by a GridBagLayout is associated with a GridBagConstraints instance that specifies how the component is laid out within its display area. 

GridBagConstraints

We can customize a GridBagConstraints object by setting one or more of its public instance variables. These variables specify the component location, size, growth factor, anchor, inset, filling, and padding.

  • gridx: An int value that specifies the leftmost cell that the component occupies. gridx specifies the column in which the component will be placed.
  • gridy: An int value that specifies the topmost cell that the component occupies. gridy specifies the row in which it will be placed.
  • gridheight: An int value that specifies the number of vertical cells that a component occupies.
  • gridwidth: An int value that specifies the number of horizontal cells that a component occupies.
  • ipadx: An int value that specifies the amount of internal horizontal padding to be added to each control.
  • ipady: An int value that specifies the amount of internal vertical padding to be added to each control.
  • insets: An Insets object that specifies the amount of empty space to leave on each side of the cell.
  • weightx: A double value that specifies how extra horizontal space is distributed if the resulting layout is horizontally smaller than the area allotted.
  • weighty: A double value that specifies how extra vertical space is distributed if the resulting layout is vertically smaller than the area allotted.
  • anchor: An int value that specifies the alignment of a component within a cell.
  • fill: An int value that specifies what to do with extra space in a cell.
  • RELATIVE: For the gridx and gridy fields, this field specifies that the component will be placed next to the last added component. For the gridwidth and gridheight fields, this field specifies that the component will be the next-to-last component in a row or column.
  • REMAINDER: For the gridwidth and gridheight fields, this field specifies that a component is the last component in a row or column.

Example

import java.awt.*;
import javax.swing.*;

public class GridBagLayoutTest {
   public static void main(String[] a) {
      JFrame frame = new JFrame("GridBagLayout Test");
      Container myPane = frame.getContentPane();
      myPane.setLayout(new GridBagLayout());
      GridBagConstraints c = new GridBagConstraints();
      setMyConstraints(c,0,0,GridBagConstraints.CENTER);
      myPane.add(getFieldPanel(),c);
      setMyConstraints(c,0,1,GridBagConstraints.CENTER);
      myPane.add(getButtonPanel(),c);
      frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      frame.setSize(300, 250);
      frame.setLocationRelativeTo(null);
      frame.setVisible(true);
   }
   private static JPanel getFieldPanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.setBorder(BorderFactory.createTitledBorder("Details"));
      GridBagConstraints c = new GridBagConstraints();
      setMyConstraints(c,0,0,GridBagConstraints.EAST);
      p.add(new JLabel("Name:"),c);
      setMyConstraints(c,1,0,GridBagConstraints.WEST);
      p.add(new JTextField(16),c);
      setMyConstraints(c,0,1,GridBagConstraints.EAST);
      p.add(new JLabel("System:"),c);
      setMyConstraints(c,1,1,GridBagConstraints.WEST);
      p.add(getSystemPanel(),c);
      setMyConstraints(c,0,2,GridBagConstraints.EAST);
      p.add(new JLabel("Language:"),c);
      setMyConstraints(c,1,2,GridBagConstraints.WEST);
      p.add(getLanguagePanel(),c);
      setMyConstraints(c,0,3,GridBagConstraints.EAST);
      p.add(new JLabel("Year:"),c);
      setMyConstraints(c,1,3,GridBagConstraints.WEST);
      p.add(new JComboBox(new String[] {"2019","2020","2021"}),c);
      return p;
   }
   private static JPanel getButtonPanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.add(new JButton("OK"));
      p.add(new JButton("Cancel"));
      return p;
   }
   private static JPanel getSystemPanel() {
      JRadioButton winButton = new JRadioButton("Windows",true);
      JRadioButton macButton = new JRadioButton("Mac",false);
      ButtonGroup systemGroup = new ButtonGroup();
      systemGroup.add(winButton);
      systemGroup.add(macButton);
      JPanel p = new JPanel(new GridBagLayout());
      p.add(winButton);
      p.add(macButton);
      return p;
   }
   private static JPanel getLanguagePanel() {
      JPanel p = new JPanel(new GridBagLayout());
      p.add(new JCheckBox("Java",true));
      p.add(new JCheckBox("Python",true));
      p.add(new JCheckBox("Spark",false));
      return p;
   }
   private static void setMyConstraints(GridBagConstraints c, int gridx, int gridy, int anchor) {
      c.gridx = gridx;
      c.gridy = gridy;
      c.anchor = anchor;
   }
}

Output


raja
Published on 10-Jul-2019 09:31:09
Advertisements