java.awt.BorderLayout is the Layout which divides component area into 5 parts which are CENTER, NORTH, SOUTH, EAST, WEST. BorderLayout is used to setup an application having contant top/bottom/left/right part. It re sizes the components added in it. Component add sequence do not play an important role. If we normally add a component in it. By default it will be added in CENTER part. To set any layout of any component the method which can be used is: componetObject.setLayout(layoutManagerObject); We have only a default constructor in java.awt.BorderLayout class.


As discussed above, We have 5 sub parts of this Layout. By default the components goes to CENTER part and replaces the previously added component. BorderLayout can accommodate at max only 5 components. To add a component in specific direction e.g. NORTH the syntax is: add(btn, BorderLayout.NORTH); So, A point to remember is: In case of BorderLayout, we need to use add() method which takes 2 arguments. One is the component to be added and another is the direction as integer. As a second argument a static int from BroderLayout can be passed as discussed below:

borderLayout with normal add() method

Program:

import java.applet.*;
import java.awt.*;

public class BorderLayoutDemo extends Applet
{
	Button b1, b2, b3;
	public void init()
	{
		BorderLayout bl = new BorderLayout(20,20);
		setLayout(bl);
		
		b1 = new Button("Button1");
		b2 = new Button("Button2");
		b3 = new Button("Button3");
		
		add(b1);
		add(b2);
		add(b3);
	}	
}
/*
<applet code="BorderLayoutDemo" width="500" height="500"></applet>
*/

Output:


BorderLayout with proper add() method

Program:

import java.applet.*;
import java.awt.*;

public class BorderLayoutAddDemo extends Applet
{
	Button b1, b2, b3;
	public void init()
	{
		BorderLayout bl = new BorderLayout();
		setLayout(bl);
		
		b1 = new Button("Button1");
		b2 = new Button("Button2");
		b3 = new Button("Button3");
		
		add(b1, BorderLayout.NORTH);
		add(b2, BorderLayout.WEST);
		add(b3);
	}	
}
/*
<applet code="BorderLayoutAddDemo" width="500" height="500"></applet>
*/

Output:


By default the center part will only be visible. If we do not add any component in any other part. CENTER part will occupy the complete area. That means, In case we have used NORTH, CENTER and WEST directions then other areas will not be created at all as shown in the applet above. To change the size occupied by any of the direction. we have to invoke a setPreferredSize() method on the component which we have added.


Re-sizing a part of BorderLayout

Program:

import java.applet.*;
import java.awt.*;

public class BorderLayoutAddResizeDemo extends Applet
{
	Button b1, b2, b3;
	public void init()
	{
		BorderLayout bl = new BorderLayout();
		setLayout(bl);
		
		b1 = new Button("Button1");
		b2 = new Button("Button2");
		b3 = new Button("Button3");
		b1.setPreferredSize(new Dimension(500,250));
		add(b1, BorderLayout.NORTH);
		add(b2, BorderLayout.WEST);
		add(b3);
	}	
}
/*
<applet code="BorderLayoutAddResizeDemo" width="500" height="500"></applet>
*/

Output:


Complete BorderLayout

Program:

import java.applet.*;
import java.awt.*;

public class BorderLayoutCompleteDemo extends Applet
{
	Button b1, b2, b3;
	public void init()
	{
		BorderLayout bl = new BorderLayout();
		setLayout(bl);
		
		b1 = new Button("Button1");
		b2 = new Button("Button2");
		b3 = new Button("Button3");
		add(b1, BorderLayout.NORTH);
		add(b2, BorderLayout.WEST);
		add(b3);
		add(new Button("Button4"),BorderLayout.SOUTH);
		add(new Button("Button5"),BorderLayout.EAST);
	}	
}
/*
<applet code="BorderLayoutCompleteDemo" width="500" height="500"></applet>
*/

Output: