Java GridBagLayout

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

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

Output:

Java Gridbaglayout 2

Next TopicJava GroupLayout

 

java for beginners
java for beginners
java for beginners
CodSir //course // books
...

WikiSir

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 […]

Tags:

....
...