Class GridBagLayout in AWT in Java

0
213
Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

Class GridBagLayout in AWT in Java:

This is an upgraded version of GridLayout.GridBagLayout implements the LayoutManager interface to layout Component objects in a Container. It is the most powerful LayoutManager.

With this, we can specify the width, height of the individual grids in a container. The GridBagLayout manager is a flexible layout manager which aligns components horizontally and vertically, without requiring the component to be of the same size.

Each GridBagLayout manager uses a rectangular grid of cells, with each component occupying one or more cells(display area). Each component in a GridBagLayout is associated with a set of constraints contained within a GridBagConstraints, instance contained within a GridBagConstraints, an instance that specifies how the component is to be laid out within its display area.

The manner in which the GridBagLayout manager places a set of components depends on each component’s constraints and it’s the minimum size as well as the preferred size of the component’s container.

It ensures that components do not overlap by adjusting the size of the cells. To use a GridBagLayout effectively, one or more components must have customized GridBagConstraints objects created for it.

The GridBagLayout LayoutManager provides the means to layout components in a flexible
grid-based display model.

The class structure of GridBagLayout is given as:

public class java.awt.GridBagLayout extends java.lang.Object implements java.awt.LayoutManager2, java.io.Serializable{
//member elements 
protected final static int MAXGRIDSIZE;
//MAXGRIDSIZE is 128 the maximum number of grid position both horizontally and vertically that 
//can be laid out by GridBagLayout .
protected final static int MINSIZE;
//MINSIZE is 1.The smallest grid that can be laid out by GridBagLayout .
protected final static int PREFERREDSIZE;
public double[] columnWeights;
public int[] columnWidths;
public int[] rowHeights;
public double[] rowWeights;
protected Hashtable comptable;
protected GridBagConstraints defaultConstraints;
protected GridBagLayoutInfo layoutInfo;
//constructor
public GridBagLayout();//creates a GridBagLayout manager
//methods
public void addLayoutComponent(String name,Component comp);
pulic void addLayoutComponent(Component comp,Object constraints);
public GridBagConstraints getConstraints(Component comp);
public void layoutContainer(Container target);
public abstract float getLayoutAignmentX(Container target);
public abstract float getLayoutAignmentY(Container target);
public Dimension minimumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutComponent(Component comp);
public void setConstraints(Component comp,GridBagConstraints constraints);
public int[][] getLayoutDimensions();
public Point getLayoutOrigin();
public double[][] getLayoutWeights();
public abstract void invalidateLayout(Container target);
public Point location(int x,int y);
public abstract Dimension maximumLayoutSize(Container target);
public String toString();
protected GridBagConstraints lookupConstraints(Component comp);
protected void adjustForGravity(GridBagConstraints constraints,Rectangle rec);
protected void arrangeGrid(Container target);
protected GridBagLayoutInfo getLayoutInfo(Container target,int sizeflag);
protected Dimension getMinSize(Container target,GridBagLayoutInfo info);
} 

The details of the class structure are given as follows:

protected static final MAXGRIDSIZE;

protected static final MAXGRIDSIZE represents the maximum number of rows and columns within container managed by GridBagLayout.

protected static final MINSIZE;

protected static final MINSIZE represents a variable that is used for internal sizing purposes.

protected static final PREFERREDSIZE;

protected static final PREFERREDSIZErepresents a variable that is used for internal sizing purposes.

public double columnWeights[];

public double columnWeights[] represents the weightx values of the components in the row with the most elements.

public int columnWidths[];

public int columnWidths[] represents the width values of the components in the row with the most elements.

public int rowHeights[];

public int rowHeights[] represents the height values of the components in the column with the most elements.

public double rowWeights[];

public double rowWeights[] represents the weighty values of the components in the column with the most elements.

protected Hashtable comptable;

protected Hashtable comptable represents a internal table to manage components.

protected GridBagConstraints defaultConstraints;

protected GridBagConstraints defaultConstraints represents a constraints to use for Components that have none.

protected GridBagLayoutInfo layoutInfo;

protected GridBagLayoutInfo layoutInfo represents a internal information about the GridBagLayout.

public GridBagLayout();

public GridBagLayout() constructs a GridBagLayout object.

public void addLayoutComponent (Component comp, Object constraints);

public void addLayoutComponent (Component comp, Object constraints) method adds the component comp to container subject to the given constraints. This is a more generalized version of addLayoutComponent(String, Component). It corresponds to java.awt.Container’s add(Component, Object).

Parameter
comp – The component being added.
constraints – An object describing the constraints on this component.

public void addLayoutComponent (String name, Component component);

public void addLayoutComponent (String name, Component component) method actually does nothing.

Parameter
name – Name of component to add.
component – Actual component being added.

public GridBagConstraints getConstraints (Component component);

public GridBagConstraints getConstraints (Component component) method returns the GridBagConstraints for component requested.

This method returns GridBagConstraints for component requested.

Parameter
component – Component whose constraints are desired

public abstract float getLayoutAlignmentX(Container target);

public abstract float getLayoutAlignmentX(Container target) method returns the preferred alignment of the given container target. A return value of 0 is left aligned, .5 is centered, and 1 is right aligned.

This method returns the value .5 for all containers.

Parameter
target – The container to inspect.

public abstract float getLayoutAlignmentY(Container target);

public abstract float getLayoutAlignmentY(Container target) method returns the preferred alignment of the given container target. A return value of 0 is top aligned, .5 is centered, and 1 is bottom aligned.

This method returns the value .5 for all containers.

Parameter
target – The container to inspect.

public int[][] getLayoutDimensions();

public int[][] getLayoutDimensions() method returns two single dimension arrays as a multi-dimensional array. Index 0 is an array of widths (columnWidths instance variable), while index 1 is an array of heights (rowHeights instance variable).

This method returns two single dimension arrays as a multi-dimensional array. Index 0 is an array of widths (columnWidths instance variable), while index 1 is an array of heights (rowHeights instance variable).

public Point getLayoutOrigin();

public Point getLayoutOrigin() method returns the origin of the components within the Container whose LayoutManager is GridBagLayout.

This method returns the origin of the components within the Container whose LayoutManager is GridBagLayout.

public double[][] getLayoutWeights();

public double[][] getLayoutWeights() method returns two single dimension arrays as a multi-dimensional array. Index 0 is an array of columns weights (columnWeights instance variable), while index 1 is an array of row weights (rowWeights instance variable).

This method returns two single dimension arrays as a multi-dimensional array. Index 0 is an array of columns weights (columnWeights instance variable), while index 1 is an array of row weights (rowWeights instance variable).

public abstract void invalidateLayout(Container target);

public abstract void invalidateLayout(Container target) method actually does nothing.

Don't miss out!
Subscribe To Newsletter

Receive top technical news, lesson ideas, travel tips and more!

Invalid email address
Give it a try. You can unsubscribe at any time.

Spread the love
  • 29
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    138
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here