Add to Google Reader or Homepage

Java BorderLayout - An Example

BorderLayout

 

Border Layout is one of the Layouts available in java to arrange the components in the Containers like JFrame and JPanel. Lets see,how to use the BorderLayout  in this post.

This Layout helps us to position the components in the Container but does not provide much flexibility compared to other Layouts like FlowLayout and GridBagLayout in rendering the Components.

Because flowlayout enables us to specify the exact coordinate Position of the component and GridBagLayout is more flexible but quite complex compared to other layouts.


 This Layout has five Constants like 

BorderLayout.NORTH - To render the components in the Northern (or Top) position of the container.

BorderLayout.SOUTH- To render the elements in the Southern (or bottom) position of the container.

BorderLayout.CENTER- To render the elements in the Center of the Container.

BorderLayout.EAST- To lay the components in the left side of the Container.

BorderLayout.WEST- To lay the elements in the right side of the container


If you do not explicitly mention the position of the components then center position will be chosen.

Look at the following program,

Program:


Borderframe.java

import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.imageio.*;
import java.io.*;
class Borderframe extends JFrame
{
public Borderframe()
{
setTitle("BorderLayoutDemo");
setSize(200,200);
setLocationByPlatform(true);

JButton b1=new JButton("B1");
JButton b2=new JButton("B2");
JButton b3=new JButton("B3");
JButton b4=new JButton("B4");
JButton b5=new JButton("B5");
add(b1,"Center");
add(b2,"North");
add(b3,"West");
add(b4,"South");
add(b5,"East");
}
}


 
 
Borderframemain.java

import javax.swing.*;
import java.awt.*;

class Borderframemain
{
public static void main(String args[])throws Exception
{
Borderframe f1=new Borderframe();
f1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f1.setVisible(true);
}
}


 
Here i have created five buttons for each of the five positions in the frame and if you compile and execute the program then you will get the output as follows.





If you see the output you can be able to infer one thing that these components(buttons) occupies the entire region of the frame in the given position.For example,Button b2 occupies the entire Northern region resulting in a big button.If the frame size is large then you would get even larger button.

But in a good UI design we would not want Buttons like these. So,in these kind of situations FlowLayout helps us to deal with  this problem. But using FlowLayout needs us to know the exact Coordinates to locate the components.

So we need to combine  these two layouts to produce the desired output.How to combine?

1.First create a panel which has default layoutmanager as FlowLayout and then add the component to the panel.

2.Next add the panel into the JFrame to the desired location.

Though you combine this two layouts you would not get the desired  output because the components at CENTER seems to be slightly closer to the Northern region as follows.





So you have to set the horizontal and vertical distance between the components in order to get the desired result using the BorderLayout(int hori-dist,int vert-dist) constructor.

I have edited the above program to produced the desired output and it as follows,



Borderframe.java

import javax.swing.*;
import java.awt.*;
import java.util.*;
import javax.imageio.*;
import java.io.*;
class Borderframe extends JFrame
{
public Borderframe()
{
setTitle("BorderLayoutDemo");
setSize(200,200);
setLayout(new BorderLayout(0,20));//horiz-dist=0 and vert-dist=20
setLocationByPlatform(true);
 

JPanel p[]=new JPanel[5];
for(int i=0;i<5 br="br" i="i">p[i]=new JPanel();


JButton b[]=new JButton[5];
for(int i=0;i<5 br="br" i="i">b[i]=new JButton("B"+i);


for(int i=0;i<5 br="br" i="i">p[i].add(b[i]);
 

add(p[0],"Center");
add(p[1],"North");
add(p[2],"West");
add(p[3],"South");
add(p[4],"East");

}

}
 



OUTPUT 







  Note:


1. The ContentPane of any JFrame has BorderLayout as the default Layout Manager.

2. The Center Component will not be rendered until all the surrounding components are drawn.


 
 
java errors and exceptions © 2010 | Designed by Chica Blogger | Back to top