java GridBagLayout

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

Java GridBagLayout

The Java GridBagLayout class is used to align components vertically, horizontally or along their baseline.

The components may not be of same size. Each GridBagLayout object maintains a dynamic, rectangular grid of cells. Each component occupies one or more cells known as its display area. Each component associates an instance of GridBagConstraints. With the help of constraints object we arrange component's display area on the grid. The GridBagLayout manages each component's minimum and preferred sizes in order to determine component's size.

Fields

Modifier and Type

Field

Description

double[]

columnWeights

It is used to hold the overrides to the column weights.

int[]

columnWidths

It is used to hold the overrides to the column minimum width.

protected Hashtable

comptable

It is used to maintains the association between a component and its gridbag constraints.

protected GridBagConstraints

defaultConstraints

It is used to hold a gridbag constraints instance containing the default values.

protected GridBagLayoutInfo

layoutInfo

It is used to hold the layout information for the gridbag.

protected static int

MAXGRIDSIZE

No longer in use just for backward compatibility

protected static int

MINSIZE

It is smallest grid that can be laid out by the grid bag layout.

protected static int

PREFERREDSIZE

It is preferred grid size that can be laid out by the grid bag layout.

int[]

rowHeights

It is used to hold the overrides to the row minimum heights.

double[]

rowWeights

It is used to hold the overrides to the row weights.

 

Useful Methods

Modifier and Type

Method

Description

void

addLayoutComponent(Component comp, Object constraints)

It adds specified component to the layout, using the specified constraints object.

void

addLayoutComponent(String name, Component comp)

It has no effect, since this layout manager does not use a per-component string.

protected void

adjustForGravity(GridBagConstraints constraints, Rectangle r)

It adjusts the x, y, width, and height fields to the correct values depending on the constraint geometry and pads.

protected void

AdjustForGravity(GridBagConstraints constraints, Rectangle r)

This method is for backwards compatibility only

protected void

arrangeGrid(Container parent)

Lays out the grid.

protected void

ArrangeGrid(Container parent)

This method is obsolete and supplied for backwards compatibility

GridBagConstraints

getConstraints(Component comp)

It is for getting the constraints for the specified component.

float

getLayoutAlignmentX(Container parent)

It returns the alignment along the x axis.

float

getLayoutAlignmentY(Container parent)

It returns the alignment along the y axis.

int[][]

getLayoutDimensions()

It determines column widths and row heights for the layout grid.

protected GridBagLayoutInfo

getLayoutInfo(Container parent, int sizeflag)

This method is obsolete and supplied for backwards compatibility.

protected GridBagLayoutInfo

GetLayoutInfo(Container parent, int sizeflag)

This method is obsolete and supplied for backwards compatibility.

Point

getLayoutOrigin()

It determines the origin of the layout area, in the graphics coordinate space of the target container.

double[][]

getLayoutWeights()

It determines the weights of the layout grid's columns and rows.

protected Dimension

getMinSize(Container parent, GridBagLayoutInfo info)

It figures out the minimum size of the master based on the information from getLayoutInfo.

protected Dimension

GetMinSize(Container parent, GridBagLayoutInfo info)

This method is obsolete and supplied for backwards compatibility only

Example

  1. import java.awt.Button;  
  2. import java.awt.GridBagConstraints;  
  3. import java.awt.GridBagLayout;  
  4.   
  5. import javax.swing.*;  
  6. public class GridBagLayoutExample extends JFrame{  
  7.     public static void main(String[] args) {  
  8.             GridBagLayoutExample a = new GridBagLayoutExample();  
  9.         }  
  10.         public GridBagLayoutExample() {  
  11.     GridBagLayoutgrid = new GridBagLayout();  
  12.             GridBagConstraints gbc = new GridBagConstraints();  
  13.             setLayout(grid);  
  14.             setTitle("GridBag Layout Example");  
  15.             GridBagLayout layout = new GridBagLayout();  
  16.     this.setLayout(layout);  
  17.     gbc.fill = GridBagConstraints.HORIZONTAL;  
  18.     gbc.gridx = 0;  
  19.     gbc.gridy = 0;  
  20.     this.add(new Button("Button One"), gbc);  
  21.     gbc.gridx = 1;  
  22.     gbc.gridy = 0;  
  23.     this.add(new Button("Button two"), gbc);  
  24.     gbc.fill = GridBagConstraints.HORIZONTAL;  
  25.     gbc.ipady = 20;  
  26.     gbc.gridx = 0;  
  27.     gbc.gridy = 1;  
  28.     this.add(new Button("Button Three"), gbc);  
  29.     gbc.gridx = 1;  
  30.     gbc.gridy = 1;  
  31.     this.add(new Button("Button Four"), gbc);  
  32.     gbc.gridx = 0;  
  33.     gbc.gridy = 2;  
  34.     gbc.fill = GridBagConstraints.HORIZONTAL;  
  35.     gbc.gridwidth = 2;  
  36.     this.add(new Button("Button Five"), gbc);  
  37.             setSize(300300);  
  38.             setPreferredSize(getSize());  
  39.             setVisible(true);  
  40.             setDefaultCloseOperation(EXIT_ON_CLOSE);  
  41.       
  42.         }  
  43.       
  44. }  

Output:

Java Gridbaglayout 1

Example 2

  1. public class GridBagLayoutDemo {  
  2. final static boolean shouldFill = true;  
  3. final static boolean shouldWeightX = true;  
  4. final static boolean RIGHT_TO_LEFT = false;  
  5.   
  6. public static void addComponentsToPane(Container pane) {  
  7. if (RIGHT_TO_LEFT) {  
  8. pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);  
  9. }  
  10.   
  11. JButton button;  
  12. pane.setLayout(new GridBagLayout());  
  13. GridBagConstraints c = new GridBagConstraints();  
  14. if (shouldFill) {  
  15. //natural height, maximum width  
  16. c.fill = GridBagConstraints.HORIZONTAL;  
  17. }  
  18.   
  19. button = new JButton("Button 1");  
  20. if (shouldWeightX) {  
  21. c.weightx = 0.5;  
  22. }  
  23. c.fill = GridBagConstraints.HORIZONTAL;  
  24. c.gridx = 0;  
  25. c.gridy = 0;  
  26. pane.add(button, c);  
  27.   
  28. button = new JButton("Button 2");  
  29. c.fill = GridBagConstraints.HORIZONTAL;  
  30. c.weightx = 0.5;  
  31. c.gridx = 1;  
  32. c.gridy = 0;  
  33. pane.add(button, c);  
  34.   
  35. button = new JButton("Button 3");  
  36. c.fill = GridBagConstraints.HORIZONTAL;  
  37. c.weightx = 0.5;  
  38. c.gridx = 2;  
  39. c.gridy = 0;  
  40. pane.add(button, c);  
  41.   
  42. button = new JButton("Long-Named Button 4");  
  43. c.fill = GridBagConstraints.HORIZONTAL;  
  44. c.ipady = 40;      //make this component tall  
  45. c.weightx = 0.0;  
  46. c.gridwidth = 3;  
  47. c.gridx = 0;  
  48. c.gridy = 1;  
  49. pane.add(button, c);  
  50.   
  51. button = new JButton("5");  
  52. c.fill = GridBagConstraints.HORIZONTAL;  
  53. c.ipady = 0;       //reset to default  
  54. c.weighty = 1.0;   //request any extra vertical space  
  55. c.anchor = GridBagConstraints.PAGE_END; //bottom of space  
  56. c.insets = new Insets(10,0,0,0);  //top padding  
  57. c.gridx = 1;       //aligned with button 2  
  58. c.gridwidth = 2;   //2 columns wide  
  59. c.gridy = 2;       //third row  
  60. pane.add(button, c);  
  61. }  
  62.   
  63.   
  64. private static void createAndShowGUI() {  
  65. //Create and set up the window.  
  66. JFrame frame = new JFrame("GridBagLayoutDemo");  
  67. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  68.   
  69. //Set up the content pane.  
  70. addComponentsToPane(frame.getContentPane());  
  71.   
  72. //Display the window.  
  73. frame.pack();  
  74. frame.setVisible(true);  
  75. }  
  76.   
  77. public static void main(String[] args) {  
  78. javax.swing.SwingUtilities.invokeLater(new Runnable() {  
  79. public void run() {  
  80. createAndShowGUI();  
  81. }  
  82. });  
  83. }  
  84. }  

Output:

Java Gridbaglayout 2