BorderLayout in Java

0
514
Spread the love
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

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.

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
  • 24
  • 34
  • 28
  • 35
  •  
  •  
  •  
  • 12
  •  
  •  
    133
    Shares

LEAVE A REPLY

Please enter your comment!
Please enter your name here