BorderLayout in Java

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

BorderLayout in Java
BorderLayout in Java

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 first argument and that String must specify with proper capitalization.

So, 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 outs a container to contain number of GUI elements like buttons,labels,grids,texts etc.The components are arranged along the edges and in center of the given container.

The hgap and vgaps arguments represents 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.LayoutManager{
//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();
}

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. Foe 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.

BorderLayout Method details:

public BorderLayout()

This method creates a new BorderLayout without any spacing or gap between the components.

public BorderLayout(int hgap,int vgap);

This method creates a new borderlayout with the specified horizontal and vertical gaps. The specified space between the components(gaps).

void addLayoutComponent(Component comp, Object constraints);

Adds the specified component to the layout using the constraints object.

void addLayoutComponent(String name, Component comp);

This method adds the String to the specified component, however as per oracle java guideline this method is deprecated now. Instead we can use addLayoutComponent(Component, Object).

Object getConstraints(Component comp);

This method returns the constraints for the specified component.

int getHgap();

This method returns the horizontal gap between components.

float getLayoutAlignmentX(Container parent);

This method returns the alignment along the x axis.

float getLayoutAlignmentY(Container parent);

This method returns the alignment along the y axis.

Component getLayoutComponent(Container target, Object constraints);

This method returns the component that corresponds to the given constraint location based on the target Container’s component orientation.

Component getLayoutComponent(Object constraints);

This method gets the component that was added using the given constraint

int getVgap();

This method returns the vertical gap between components.

void invalidateLayout(Container target)

This method invalidates the layout,that means the layout manager should discard the cached information.

void layoutContainer(Container target)

This method lays out the container argument using this border layout.

Dimension maximumLayoutSize(Container target)

This method returns the maximum dimensions for this given components in the specified target container.

Dimension minimumLayoutSize(Container target)

This method Determines the minimum size of the target container using this layout manager.

Dimension preferredLayoutSize(Container target)

This method determines the preferred size of the target container using this layout manager, based on the components in the  container.

void removeLayoutComponent(Component comp)

This method removes the specified component from this border layout.

void setHgap(int hgap)

This method sets the horizontal gap between components.

void setVgap(int vgap)

This method sets the vertical gap between components.

String toString()

This method Returns a string representation of the state of this border layout.

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

  • clone()
  • equals()
  • finalize()
  • getClass()
  • hashCode()
  • notify()
  • notifyAll()
  • wait()
  • wait(long)
  • wait(long,int)
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