1. Introduction
The Border Layout Manager sets the components in a predefined region of the container. These are listed below:
- NORTH
- SOUTH
- EAST
- WEST
- CENTER
Since this Layout Manager arranges the controls in the container’s border, it got the name, Border Layout. This Manager is suitable for fixing the scroll bars and is default for AWT Windows, Frames and Dialog boxes. A developer while adding the control can tell the Layout Manger to place it in a specific location said above. One can either point out these regions by using the constants in the BorderLayout Class or string literals that denote these locations.
The regions on the four sides takes up the area needed for the control. The CENTER region eats up the rest. The control in the NORTH and SOUTH region runs horizontally. Whereas, the control in the EAST and WEST regions grows vertically. In this example, we will set up two labels, two buttons and a Text Area then lay them out using the Border Layout Manager. The hub pages network article is useful for knowing the basics of AWT Frame Window.
2. Creating The Frame Window
First, we will create the Frame Window for this example. Our example requires the below java classes which we import into our java file.
1 2 3 4 |
//Sample 01a: Required Imports for this Example import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; |
Next, we give a title to our Frame Window and also specify it initial size and location. Also, we hook this Window to a listener so we can close the Frame Window using the X button in the top right corner.
1 2 3 4 5 |
//Sample 02: Setup the Frame Window super(FrameTitle); setSize(500, 400); setLocation(100,100); addWindowListener(this); |
3. Using Border Layout Manager
We will create five components and ask Border Layout to organize them in the Frame Window. We create two Labels and two buttons. Also, we set yellow background color to the Labels. The TextArea component supports multi-line text. We create that also for our example Frame Window. While adding this to Frame Windows, we will specify the CENTER region.
1 2 3 4 5 6 7 8 9 10 |
//Sample 05: Create Two Labels and Two Buttons Label lb1 = new Label("Top of the Layout"); Label lb2 = new Label("Bottom of the Layout"); Button b1 = new Button("L"); Button b2 = new Button("R"); lb1.setBackground(Color.YELLOW); lb2.setBackground(Color.YELLOW); //Sample 06: Create a Text Area Control TextArea ta = new TextArea(); |
Next, we create BorderLayout object and handover that to our Frame Window. The setLayout method assigns the Layout Manager for the container. In our case, the container is a Frame Window. Now, the Border Layout Manager takes care of control layout for our Frame Window.
1 2 3 |
//Sample 07: Create a Border Layout for Frame BorderLayout layout = new BorderLayout(); setLayout(layout); |
Finally, we add the five components which we created already to the Frame Window using the add method. Note, the region string in the second parameter tells the layout manager where the control should stay. A region can hold only one component.
1 2 3 4 5 6 |
//Sample 08: Add these controls to the Container add(lb1, "North"); add(lb2, "South"); add(b1, "East"); add(b2, "West"); add(ta, "Center"); |
4. Complete Code Example and Output
AwtBorderLayoutExample.java
1 2 3 4 5 6 7 |
public class AwtBorderLayoutExample { public static void main(String[] args) { //Sample 04: Create Frame and Display it BorderLayoutWindow fw = new BorderLayoutWindow("AWT Border Layout Example"); fw.setVisible(true); } } |
BorderLayoutWindow.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
//Sample 01a: Required Imports for this Example import java.awt.Frame; import java.awt.event.WindowEvent; import java.awt.event.WindowListener; //Sample 01b: Required Imports for this Example import java.awt.BorderLayout; import java.awt.Label; import java.awt.Button; import java.awt.Color; import java.awt.TextArea; public class BorderLayoutWindow extends Frame implements WindowListener { public BorderLayoutWindow(String FrameTitle){ //Sample 02: Setup the Frame Window super(FrameTitle); setSize(500, 400); setLocation(100,100); addWindowListener(this); //Sample 05: Create Two Labels and Two Buttons Label lb1 = new Label("Top of the Layout"); Label lb2 = new Label("Bottom of the Layout"); Button b1 = new Button("L"); Button b2 = new Button("R"); lb1.setBackground(Color.YELLOW); lb2.setBackground(Color.YELLOW); //Sample 06: Create a Text Area Control TextArea ta = new TextArea(); //Sample 07: Create a Border Layout for Frame BorderLayout layout = new BorderLayout(); setLayout(layout); //Sample 08: Add these controls to the Container add(lb1, "North"); add(lb2, "South"); add(b1, "East"); add(b2, "West"); add(ta, "Center"); } //Sample 03: Implement the Listeners public void windowOpened(WindowEvent e) {} public void windowClosing(WindowEvent e) { this.dispose(); } public void windowClosed(WindowEvent e) {} public void windowIconified(WindowEvent e) {} public void windowDeiconified(WindowEvent e) {} public void windowActivated(WindowEvent e) {} public void windowDeactivated(WindowEvent e) {} } |
Output

Tags: AWT, BorderLayout