Learn LayoutManager in Java Quick and Easy Way in 2020

Class Class in Java

Abstract Interface LayoutManager in Java

LayoutManager is an interface that defines the responsibilities of an object that wants to lay out Components to the display in a Container.

The structure of the abstract interface LayoutManager is given by

public abstract interface java.awt.LayoutManager {
// Interface Methods
public abstract void addLayoutComponent (String name,
Component component);
public abstract void layoutContainer (Container target);
public abstract Dimension minimumLayoutSize (Container target);
public abstract Dimension preferredLayoutSize (Container target);
public abstract void removeLayoutComponent (Component component);
}

The details of the class structure are given as follows:

Ads code goes here
public abstract void addLayoutComponent (String name,Component component);

public abstract void addLayoutComponent (String name,Component component) method adds an object to a container when Container.add(String, Component) is called.

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

public abstract void layoutContainer (Container target);

public abstract void layoutContainer (Container target) method is called when target needs to be redrawn.

Parameter
target – The container who needs to be redrawn.

public abstract Dimension minimumLayoutSize (Container target);

public abstract Dimension minimumLayoutSize (Container target) method is called when the minimum size of the target container needs to be calculated.

This method returns the minimum Dimension of the container target

Parameter
target – The container whose size needs to be calculated.

public abstract Dimension preferredLayoutSize (Container target);

public abstract Dimension preferredLayoutSize (Container target) method is called when the preferred size of the target container needs to be calculated.

This method returns the preferred Dimension of the container target

Parameter
target – The container whose size needs to be calculated.

public abstract void removeLayoutComponent (Component component);

public abstract void removeLayoutComponent (Component component) method remove a component from thelayout when Container.remove(Component) is called.

Parameter
component – Component to no longer track.

Abstract interface LayoutManager2 in Java

LayoutManager2 interface is an extension of the LayoutManager interface. It defines additional layout management methods for layout managers that perform constraint-based layout.

It provides a more generalized way to add components to a container, as well as more sizing and alignment methods.

GridBagLayout is an example of a constraint-based layout manager – each component added to the layout is associated with a GridBagConstraints object that specifies the constraints ” on how the component is to be laid out.

Java programs do not directly invoke the methods of this interface – – they are used by the Container object for which the layout manager is registered.

public abstract interface java.awt.LayoutManager2 extends java.awt.LayoutManager (
// Public Instance Methods
public abstract void addLayoutComponent(Component comp, Object constraints);
public abstract float getLayoutAlignmentX(Container target);
public abstract float getLayoutAlignmentY(Container target);
public abstract void invalidateLayout(Container target);
public abstract Dimension maximumLayoutSize(Container target);
}

The details of the class structure are given as follows:

public abstract void addLayoutComponent (Component comp,Object constraints);

public abstract void addLayoutComponent (Component comp,Object constraints) method adds an object to a container.This is slightly more generic than LayoutManager’s addLayoutComponent(String, Component).

Parameter
comp – Component to add.
constraints – Constraints on the component.

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 a value between 0 and 1.

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 a value between 0 and 1.

Parameter
target – The container to inspect.

public abstract void invalidateLayout(Container target);

public abstract void invalidateLayout(Container target) method can be used to signal the manager to discard any cached information and start fresh. This method may cache information to improve performance

Parameter
target – The container to invalidate.

public abstract Dimension maximumLayoutSize(Container target);

public abstract Dimension maximumLayoutSize(Container target) method returns the maximum size of target using this layout manager.

This method returns the maximum size of the target using this layout manager.

Parameter
target – The container to inspect.

Different layoutmanagers in Java are as follows:

Class FlowLayout in AWT in Java

FlowLayourt implements the LayoutManager interface to layout Component objects in a Container. The most basic and default layout in java is FlowLayout.

The manager implements a simple Layout style which is similar to how words flow in a text editor. Components are laid from the upper left corner, left to right and top to bottom. When no more components fit on a line, the next one will appear in the next line. It fits as many components as it can in a row before moving onto the next row.

The constructor allows us to specify one of the three constants as an alignment value for the rows and also allows us to specify the horizontal spacing between components and vertical spacing between the rows.

A small space is left between components, above and below. However, the user can change this gap, alignment, etc. The default gap is five pixels and the default alignment is center. Below are the different functions for FlowLayout.FlowLayout is typically used to arrange buttons in a panel.

Any application should never call the LayoutManager methods of this class directly. The Container for which the FlowLayout is registered does all on its own.

In short, the FlowLayout LayoutManager provides the means to layout components in a row by row fashion. As each row fills up, the components continue on the next row.

The class structure of class FlowLayout is given as:

public class java.awt.FlowLayout extends java.lang.Object implements java.awt.LayoutManager,java.io.Serializable{
//member elements
public final static int CENTER;
//this indicates that each row of components should be at center.
public final static int LEFT;
//this indicates that each row of components should be at left justified.
public final static int RIGHT;
//this indicates that each row of components should be at right justified.
//constructor
public FlowLayout();
public FlowLayout(int alignment);//alignment can be LEFT,CENTER,RIGHT
public FlowLayout(int alignment,int hgap,int vgap));//alignment can be LEFT,CENTER,RIGHT
//hgap and vgap are the space between two components horizontally and vertically.
//methods
public void addLayoutComponent(String name,Component comp);//From LayoutManager
public void layoutContainer(Container target);//From LayoutManager
public int getAlignment();
public int getHgap();
public int getVgap();
public Dimension minimumLayoutSize(Container target)//From LayoutManager
public Dimension preferredLayoutSize(Container target)//From LayoutManager
public void removeLayoutComponent(Component comp);
public void setAlignment(int allignment);
public void setHgap(int hgap);
public void setVgap(int vgap);
public String toString();
} 

The details of the class structure are given as follows:

public static final int CENTER;

public static final int CENTER represents the default alignment for a FlowLayout object; rows of components are centered within the container.

public static final int LEFT;

public static final int LEFT represents an alignment for a FlowLayout object; rows of components start on the left side of the container.

public static final int RIGHT;

public static final int RIGHT represents an alignment for a FlowLayout object; rows of components start on the right side of the container.

public FlowLayout();

public FlowLayout() constructs a FlowLayout object with CENTER alignment.

public FlowLayout (int alignment);

public FlowLayout (int alignment) constructs a FlowLayout object with the given alignment.

Parameter
alignment – Alignment of components within the container.

public FlowLayout (int alignment, int hgap, int vgap);

public FlowLayout (int alignment, int hgap, int vgap) constructs a FlowLayout object with the given alignment and the values specified as the gaps between each component in the container managed by this instance of FlowLayout.

Parameter
alignment – Alignment of components within container
hgap – Horizontal space between each component in a row
vgap – Vertical space between each row

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 int getAlignment();

public int getAlignment() method returns the alignment constant for this FlowLayout.

This method returns the alignment constant for this FlowLayout.

public int getHgap();

public int getHgap() method returns the horizontal gap between components.

This method returns the horizontal gap between components.

public int getVgap();

public int getVgap() method returns the vertical gap between components.

This method returns the vertical gap between components.

public void layoutContainer (Container target);

public void layoutContainer (Container target) method draws the components contained within the target container.

Parameter
target – The container that needs to be redrawn.

public Dimension minimumLayoutSize (Container target);

public Dimension minimumLayoutSize (Container target) method calculates minimum size of target container.

This method returns the minimum Dimension of container target

Parameter
target – The container whose size needs to be calculated.

public Dimension preferredLayoutSize (Container target);

public Dimension preferredLayoutSize (Container target) method calculates preferred size of target container.

This method returns the preferred Dimension of container target

Parameter
target – The container whose size needs to be calculated.

public void removeLayoutComponent (Component component);

public void removeLayoutComponent (Component component) method actually does nothing.

Parameter
target – Component to stop tracking.

public void setAlignment (int align);

public void setAlignment (int align) method sets the alignment for the FlowLayout.

Parameter
align – Alignment of components within container

public void setHgap (int hgap);

public void setHgap (int hgap) method sets the horizontal gap between components.

Parameter
hgap – The horizontal gap value.

public void setVgap (int vgap);

public void setVgap (int vgap) method sets the vertical gap between components.

Parameter
vgap – The vertical gap value.

public String toString();

public String toString() method returns a string representation of the FlowLayout object.

This method returns a string representation of the FlowLayout object.

an example:


component.setLayout(new FlowLayout());
for(int i=0;i<20;i++);
component.add(New Button("OK"));

Components will be compacted to their smallest size in a FlowLayout.

READ  Class PipedWriter Class in Java

GridLayout in AWT in Java:

GridLayout provides flexibility for placing components by implementing the LayoutManager interface to layout component objects in a container. It allows us to build a table of components and as we add them, they are placed left to right and top to bottom in the grid. So, It causes the container’s component to be laid out in a rectangular grid.

The container is split into equal-sized rectangles. If either the number of rows or the number of columns is set to zero, its value is computed from other dimensions and the total number of components. In GridLayout the components are resized to fill the row-column area.

If it allows, GridLayout can reposition or resize objects after adding or removing components. Whenever the area is resized, the components within the area are also resized.

One component is placed into each rectangle. We can create the manager with a number of rows and columns. Components then fill up the calls defined by the manager. In the constructor, we need to specify the number of rows and columns that we need and these are laid out in equal proportion.

The GridLayout LayoutManager provides the means to layout components in a grid of rows and columns.

The class structure of class GridLayout is given below:

public class java.awt.GridLayout extends java.lang.Object implements java.awt.LayoutManager,                                 java.io.Serializable{
//constructor
public GridLayout();
public GridLayout(int rows,int cols);
public GridLayout(int rows,int cols,int hgap,int vgap);
//methods
public void addLayoutComponent(String name,Component comp);
public void layoutContainer(Container target);
public Dimension minimumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutSize(Container target);
public void removeLayoutComponent(Component comp);
public int getColumns();
public void setColumns(int cols);
public int getHgap();
public void setHgap(int hgap);
public int getRows();
public void setRows(int rows);
public int getVgap();
public void setVgap(int vgap);
public String toString();
} 

The details of the class structure are given as follows:

public GridLayout();

public GridLayout() constructs a GridLayout object with a default single row and one column per component.

public GridLayout (int rows, int cols);

public GridLayout (int rows, int cols) constructs a GridLayout object with the requested number of rows and columns. Note that the actual number of rows and columns depends on the number of objects in the layout, not the constructor’s parameters.

Parameter
rows – Requested number of rows in the container.
cols – Requested number of columns in container.

public GridLayout (int rows, int cols, int hgap, int vgap);

public GridLayout (int rows, int cols, int hgap, int vgap) constructs a GridLayout object with the requested number of rows and columns and the values specified as the gaps between each component. Note that the actual number of rows and columns depends on the number of objects in the layout, not the constructor’s parameters.

Parameter
rows – Requested number of rows in the container.
cols – Requested number of columns in the container.
hgap – Horizontal space between each component in a row.
vgap – Vertical space between each row.

public void addLayoutComponent (String name, Component component);

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

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

public int getColumns();

public int getColumns() method returns the number of columns.

This method returns the number of columns.

public int getHgap();

public int getHgap() method returns the horizontal gap for this GridLayout instance.

This method returns the horizontal gap for this GridLayout instance.

public int getRows();

public int getRows() method returns the number of rows.

This method returns the number of rows.

public int getVgap();

public int getVgap() method returns the vertical gap for this GridLayout instance.

This method returns the vertical gap for this GridLayout instance.

public void layoutContainer (Container target);

public void layoutContainer (Container target) method draws the components contained within the target.

Parameter
target – The container that needs to be redrawn.

public Dimension minimumLayoutSize (Container target);

public Dimension minimumLayoutSize (Container target) method Calculates the minimum size of the target container.

Parameter
target – The container whose size needs to be calculated.

public Dimension preferredLayoutSize (Container target);

public Dimension preferredLayoutSize (Container target) method calculates the preferred size of the target container.

This method returns the Preferred Dimension of the container target.
Parameter
target – The container whose size needs to be calculated.

public void removeLayoutComponent (Component component);

public void removeLayoutComponent (Component component) method actually does nothing.

Parameter
component – Component to stop tracking.

public void setColumns(int cols);

public void setColumns(int cols) method sets the number of columns

Parameter
cols – The new number of columns.

public void setHgap(int hgap);

public void setHgap(int hgap) method sets the horizontal gap between components.

Parameter
hgap – The horizontal gap value.

public void setRows(int rows);

public void setRows(int rows) method sets the number of rows.

Parameter
rows – The new number of rows.

public void setVgap(int vgap);

public void setVgap(int vgap) method sets the vertical gap between components.

Parameter
vgap – The vertical gap value.

public String toString();

public String toString() method returns a string representation of the GridLayout object.

This method returns a string representation of the GridLayout object.

public GridLayout(int rows,int cols);
creates a grid layout with the specified number of rows and columns. All components in the layout are given an equal size. One but not both rows and columns can be zero, which means that any number of objects can be placed in a row or in a column.
public GridLayout(int rows,int cols,int hgap,int vgap);
creates a grid layout with the specified number of rows and columns. All components in the layout are given an equal size. In addition, the horizontal and vertical gaps are placed at the left and right edge and between each of the columns. The vertical gaps are placed at the top and bottom edge and between each row. One but not both rows and columns can be zero, which means that any number of objects can be placed in a row or in a column.
Example


Button b1,b2,b3,b4,b5,b6;
Frame frame=new Frame();
frame.setLayout(new GridLayout(3,2));//3 rows and 2 columns
b1=new Button("B1");
b2=new Button("B2");
b3=new Button("B3");
b4=new Button("B4");
b5=new Button("B5");
b6=new Button("B6");
frame.add(b1);frame.add(b2);frame.add(b3);
frame.add(b4);frame.add(b5);frame.add(b6);
 

In case if the last slot is empty, no balancing goes on with GridLayout.

Class CardLayout in Java

This is unique from other layout managers in the sense that it represents several different layouts as living on separate index cards that can be shuffled so that anyone’s card is on top at a given point of time.

The CardLayout LayoutManager provides the means to manage multiple components, displaying one at a time. Components are displayed in the order in which they are added to the layout, or in an arbitrary order by using an assignable name.

That means only one card is visible at a time, allowing applications to flip through the cards. This is useful for the user interface that has optional components that can be dynamically enabled or disabled upon user input.

The structure of the class CardLayout is given as:


public class java.awt.CardLayout extends java.lang.Object implements java.awt.LayoutManager2,java.io.Serializable{
//constructors
public CardLayout();//default constructor ,creates a new card layout
public CardLayout(int hgap,int vgap);
//creates a new card layout with the specified horizontal and vertical gaps. The specified 
//space between the components(gaps)The horizontal gaps are placed at the left and right edge.
//The vertical gaps are placed at the top and bottom edge.
//methods:
public void addLayoutComponent (Component comp,Object constraints);
public void addLayoutComponent(String name,Component comp);
public void first(Container target);
public void last(Container target);
public void next(Container target);
public void previous(Container target);
public int getHgap();
public int getVgap();
public abstract void invalidateLayout(Container target);
public abstract float getLayoutAlignmentX(Container target);
public abstract float getLayoutAlignmentY(Container target);
public void layoutContainer(Container target);
public Dimension minimumLayoutSize(Container target);
public abstract Dimension maximumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutSize(Container target);
public void setHgap (int hgap);
public void setVgap (int vgap);
public void show(Container target,String name);
public String toString();
} 

The details of the class structure are given as follows:

public CardLayout();

public CardLayout() constructs a CardLayout object.

public CardLayout (int hgap, int vgap);

public CardLayout (int hgap, int vgap) Constructs a CardLayout object with the values specified as the gaps around the container managed by this instance of CardLayout.

Parameter
hgap – Horizontal space around left and right of container
vgap – Vertical space around top and bottom of container

public void addLayoutComponent (Component comp,Object constraints);

public void addLayoutComponent (Component comp,Object constraints) method Adds the component comp to a 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). In practice, it is used the same in Java 1.1 as in Java 1.0.2, except with the parameters swapped:

Panel p = new Panel();
p.setLayoutManager(new CardLayout());
p.add(new Button("OK"), "My Test");

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 Places component under the layout’s management, assigning it the given name. This has been replaced in version 1.1 with the more general addLayoutComponent(Component,Object).

Parameter
name – Name of the component to add.
component – The actual component being added.

public void first (Container parent);

public void first (Container parent) method sets the container to display the first component in parent.

Parameter
parent – The container whose displayed component is changing.

public int getHgap();

public int getHgap() method returns the horizontal gap for this CardLayout instance.

This method returns the horizontal gap for this CardLayout instance.

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 getVgap();

public int getVgap() method returns the vertical gap for this CardLayout instance.

This method returns the vertical gap for this CardLayout instance.

public abstract void invalidateLayout(Container target);

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

READ  Class ZipEntry in Java

Parameter
target – The container to invalidate.

public void last(Container parent);

public void last (Container parent) method sets the container to display the final component in parent.

Parameter
parent – The container whose displayed component is changing.

public void layoutContainer(Container target);

public void layoutContainer (Container target) method displays the currently selected component contained within target.

Parameter
target – The container that needs to be redrawn.

public abstract Dimension maximumLayoutSize(Container target);

public abstract Dimension maximumLayoutSize(Container target) method returns a maximal Dimension for CardLayout.

This method returns a Dimension whose horizontal and vertical components are Integer.MAX_VALUE.

Parameter
target – The container to inspect.

public Dimension minimumLayoutSize (Container target);

public Dimension minimumLayoutSize (Container target) method calculates minimum size of the target container.

This method returns the minimum Dimension of the container target.

Parameter
target – The container whose size needs to be calculated.

public void next (Container parent);

public void next (Container parent) method sets the container to display the following component in the parent.

Parameter
parent – The container whose displayed component is changing.

public Dimension preferredLayoutSize (Container target);

public Dimension preferredLayoutSize (Container target) method calculates preferred size of the target container.

This method returns the preferred Dimensions of the container target.

Parameter
target – The container whose size needs to be calculated.

public void previous (Container parent);

public void previous (Container parent) method sets the container to display the prior component in parent.

Parameter
parent – The container whose displayed component is changing.

public void removeLayoutComponent (Component component);

public void removeLayoutComponent (Component component) method removes component from the layout manager’s internal tables.

Parameter
component – Component to stop tracking.

public void setHgap (int hgap);

public void setHgap (int hgap) method sets the horizontal gap for the left and right of the container.

Parameter
hgap – The horizontal gap value.

public void setVgap (int vgap);

public void setVgap (int vgap) method sets the vertical gap for the top and bottom of the container.

Parameter
vgap – The vertical gap value.

public void show (Container parent, String name);

public void show (Container parent, String name) method sets the container to display the component name in parent.

Parameter
parent – The container whose displayed component is changing.
name – Name of component to display.

public String toString();

public String toString() method returns a string representation of the CardLayout object.

This method returns a string representation of the CardLayout object.

An Example


Frame frame=new Frame();
CardLayout layout=new CardLayout();
frame.setLayout(layout);
frame.add("1",new Button("Card1"));
frame.add("2",new Button("Card2"));
frame.add("3",new Button("Card3"));
frame.add("4",new Button("Card4"));
public boolean keyDown(Event e,int key){
layout next(this);
return true;
}
 

add() method needs labels to pass a card name. We can refer to the cards by card name. Here layout object is created first and then refers the same to the frame. This is because we need to refer to the CardLayout object after we have created it.

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.

Parameter
target
The container to invalidate.

public void layoutContainer (Container target);

public void layoutContainer (Container target) method draws components contained within target.

Parameter
target – The container that needs to be redrawn.

public Point location (int x, int y);

public Point location (int x, int y) method tocates the grid position in the Container under the given location.

This method returns the grid element under the location provided at position (x, y) in pixels. Note – the returned Point uses the GridBagLayout’s grid for its coordinate space.

Parameter
x – The x coordinate of the grid position to find.
y– The y coordinate of the grid position to find.

public abstract Dimension maximumLayoutSize(Container target);

public abstract Dimension maximumLayoutSize(Container target) method returns a maximal Dimension,For GridBagLayout.

This method returns a Dimension whose horizontal and vertical components are Integer.MAX_VALUE.

Parameter
target – The container to inspect.

public Dimension minimumLayoutSize (Container target);

public Dimension minimumLayoutSize (Container target) method calculates minimum size of target container.

This method returns the Minimum Dimension of container target.

Parameter
target – The container whose size needs to be calculated.

public Dimension preferredLayoutSize (Container target);

public Dimension preferredLayoutSize (Container target) method calculates preferred size of target container.

This method returns the Preferred Dimension of container target

READ  Class EOFException in Java

Parameter
target – The container whose size needs to be calculated.

public void removeLayoutComponent (Component component);

public void removeLayoutComponent (Component component) method actually does nothing.

Parameter
component – Component to stop tracking.

public void setConstraints (Component component,GridBagConstraints constraints);

public void setConstraints (Component component,GridBagConstraints constraints) method changes the GridBagConstraints on component to those provided.

Parameter
component – Component to set constraints for
constraints – Constraints for component

public String toString();

public String toString() method returns a string representation of the GridBagLayout object.

This method returns a string representation of the GridBagLayout object.

protected void AdjustForGravity (GridBagConstraints constraints,Rectangle r);

protected void AdjustForGravity (GridBagConstraints constraints,Rectangle r) method is a helper method for laying out a cell of the grid. The method adjusts the values for r based upon the constraints.

Parameter
constraints – Constraints to use for adjustment of Rectangle.
r – Rectangular area that needs to be adjusted.

protected void ArrangeGrid (Container target);

protected void ArrangeGrid (Container target) method is a helper method that does the actual arrangement of components in target.

Parameter
target – Container to layout.

protected GridBagLayoutInfo GetLayoutInfo (Container target,int sizeFlag);

protected GridBagLayoutInfo GetLayoutInfo (Container target,int sizeFlag) method returns an internal class used to help size the container.

This method returns an internal class used to help size the container.

Parameter
target – Container to get information about.
sizeFlag – One of the constants MINSIZE or PREFERREDSIZE.

protected Dimension GetMinSize (Container target,GridBagLayoutInfo info);

protected Dimension GetMinSize (Container target,GridBagLayoutInfo info) method is a helper method for calculating size of container.

This method returns the minimum Dimension of container target based on info.

Parameter
target – Container to calculate size.
info – Specifics about the container’s constraints.

protected GridBagConstraints lookupConstraints (Component comp);

protected GridBagConstraints lookupConstraints (Component comp) method is a helper method for calculating size of container.

This method returns a reference to the GridBagConstraints object for this component.

Parameter
comp – Component in question.

BorderLayout in Java

The BorderLayout class implements the LayoutManager interface to structure its component objects in a given container. This concept is useful for BorderLayout in java swing or BorderLayout in java applet or BorderLayout java tutorial.

This layout manager has the concept of four border regions and a center area. When we add something using BorderLayout, we need to call add()(container.add()) that takes a String object as the first argument and that String must specify with proper capitalization.

So, the BorderLayout manager implements a common layout style which has five zones. Each of these zones is named with a String like North, South, East, West, and Center. A BorderLayout lays out a container to contain the number of GUI elements like buttons, labels, grids, texts, etc. The components are arranged along the edges and in the center of the given container.

The hgap and vgaps arguments represent the horizontal and vertical spacing or gaping between the components.

The structure of the class BorderLayout is given as:

public class java.awt.BorderLayout extends java.lang.Objects implements java.awt.LayoutManager2,java.io.Serialization{
//Fileds
public static final String AFTER_LAST_LINE
public static final String AFTER_LINE_ENDS
public static final String BEFORE_FIRST_LINE
public static final String BEFORE_LINE_BEGINS
public static final String CENTER
public static final String EAST
public static final String NORTH
public static final String SOUTH
public static final String WEST
public static final String LINE_END
public static final String LINE_START
public static final String PAGE_END
public static final String PAGE_START
//constructors
public BorderLayout();//default constructor ,constructs a new border layout.
public BorderLayout(int hgap,int vgap);
//method
public void addLayoutComponent(String name,Component comp);
public void layoutContainer(Container target);
public Dimension minimumLayoutSize(Container target);
public Dimension preferredLayoutSize(Container target);
public void removeLayoutSize(Container target);
public String toString();
}

The details of the class structure are given as:

public static final String AFTER_LAST_LINE

public static final String AFTER_LAST_LINE represents a synonym for PAGE_END. Exists for compatibility with previous versions. PAGE_END is preferred.

public static final String AFTER_LINE_ENDS

public static final String AFTER_LINE_ENDS represents a synonym for LINE_END. Exists for compatibility with previous versions. LINE_END is preferred.

public static final String BEFORE_FIRST_LINE

public static final String BEFORE_FIRST_LINE represents a synonym for PAGE_START. Exists for compatibility with previous versions. PAGE_START is preferred.

public static final String BEFORE_LINE_BEGINS

public static final String BEFORE_LINE_BEGINS represents a synonym for LINE_START. Exists for compatibility with previous versions. LINE_START is preferred.

public static final String CENTER

public static final String CENTER represents a constant representing center orientation.

public static final String EAST

public static final String EAST represents a constant representing east orientation.

public static final String NORTH

public static final String NORTH represents a constant representing north orientation.

public static final String SOUTH

public static final String SOUTH represents a constant representing south orientation.

public static final String WEST

public static final String WEST represents a constant representing west orientation.

public static final String LINE_END

public static final String LINE_END represents that the component comes after the last line of the layout’s content. For Western, left-to-right and top-to-bottom orientations, this is equivalent to SOUTH.

public static final String LINE_START

public static final String LINE_START represents that the component goes at the beginning of the line direction for the layout. For Western, left-to-right and top-to-bottom orientations, this is equivalent to WEST.

public static final String PAGE_END

public static final String PAGE_END represents that the component comes after the last line of the layout’s content. For Western, left-to-right and top-to-bottom orientations, this is equivalent to SOUTH.

public static final String PAGE_START

public static final String PAGE_START represents that the component comes before the first line of the layout’s content. For Western, left-to-right and top-to-bottom orientations, this is equivalent to NORTH.

public BorderLayout();

public BorderLayout() Constructs a BorderLayout object.

public BorderLayout(int hgap,int vgap);

public BorderLayout(int hgap, int vgap) Constructs a BorderLayout object with the values specified as the gaps between each component in the container-managed by this instance of BorderLayout.

Parameter
hgap – Horizontal space between each component in the container.
vgap – Vertical space between each component in the container.

public void addLayoutComponent (Component comp, Object constraints);

public void addLayoutComponent (Component comp, Object constraints) method adds the component comp to a container subject to the given constraints. This is a more general version of the addLayoutComponent(String, Component) method. It corresponds to java.awt.Container’s add(Component, Object) method. In practice, it is used the same in version 1.1 as in Java 1.0.2, except with the parameters swapped:

Panel p = new Panel(new BorderLayout());
p.add(new Button("OK"), BorderLayout.SOUTH);

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 adds a component to a container in region name. This has been replaced in version 1.1 with the more general addLayoutComponent(Component, Object).

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

public int getHgap();

public int getHgap() method returns the horizontal gap for this BorderLayout instance.

This method returns the horizontal gap for this BorderLayout instance.

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 getVgap();

public int getVgap() method returns the vertical gap for this BorderLayout instance.

This method returns the vertical gap for this BorderLayout instance.

public abstract void invalidateLayout(Container target);

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

Parameter
target – The container to invalidate.

public void layoutContainer(Container target);

public void layoutContainer (Container target) method draws components contained within the target.

Parameter
target – The container that needs to be redrawn.

public abstract Dimension maximumLayoutSize(Container target);

public abstract Dimension maximumLayoutSize(Container target) method returns a Dimension whose horizontal and vertical components are Integer.MAX_VALUE.

This method returns a Dimension whose horizontal and vertical components are Integer.MAX_VALUE.

Parameter
target – The container to inspect.

public Dimension minimumLayoutSize(Container target);

public Dimension minimumLayoutSize (Container target) method calculates the minimum size of the target.container.

This method returns the minimum Dimension of the container target.

Parameter
target – The container whose size needs to be calculated.

public Dimension preferredLayoutSize(Container target);

public Dimension preferredLayoutSize (Container target) method calculates the preferred size of the target container.

This method returns the preferred Dimension of the container target.

Parameter
target – The container whose size needs to be calculated.

public void removeLayoutComponent (Component component);

public void removeLayoutComponent (Component component) method removes the component from any internal tracking systems.

Parameter
component – Component to stop tracking.

public void setHgap (int hgap);

public void setHgap (int hgap) method sets the horizontal gap between components.

Parameter
hgap – The horizontal gap value.

public void setVgap (int vgap);

public void setVgap (int vgap) method sets the vertical gap between components.

Parameter
vgap – The vertical gap value.

public String toString();

public String toString() method returns a string representation of the BorderLayout object.

This method returns a string representation of the BorderLayout object.

Example
BorderLayout in java swing: This can be applied to BorderLayout in java applet


Frame frame=new Frame();
frame.setLayout(new BorderLayout(20,40));
Button buttonN,buttonS,buttonE,buttonW,buttonC;
buttonN=new Button("North");
buttonS=new Button("South");
buttonW=new Button("West");
buttonE=new Button("East");
buttonC=new Button("Center");
frame.add("North",buttonN);
frame.add("South",buttonS);
frame.add("West",buttonW);
frame.add("East",buttonE);
frame.add("Center",buttonC);
}

BorderLayout java tutorial further:

BorderLayout relies on position string, being case sensitive passing “NORTH” or “north” instead of “North” will not work. For every placement but “Center”, the element that we add is compressed to fit in the smallest amount of space along one dimension while it is stretched to the minimum along the other dimension. “Center” however spreads out along both dimensions to occupy the middle.

Note: We should not directly call the LayoutManager methods directly. The Container for which the BorderLayout is registered calls them internally.

Apart from these methods BorderLayout class inherits the below methods from Object class.

  • clone()
  • equals()
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • wait()
  • wait(long)
  • wait(long,int)

Ref: Oracle doc

Share and Enjoy !

1 thought on “Learn LayoutManager in Java Quick and Easy Way in 2020”

Leave a Comment

Your email address will not be published. Required fields are marked *