Programming Examples

Are you a Programmer or Application Developer or a DBA? Take a cup of coffee, sit back and spend few minutes here :)

Border Layout of AWT Explained

1. Introduction

The Border Layout Manager sets the components in a predefined region of the container. These are listed below:

  • EAST
  • WEST

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.

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.

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.

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.

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.

4. Complete Code Example and Output
Java AWT Border Layout Example
Java AWT Border Layout Example

Categories: AWT, Java

Tags: ,

Do you like this Example? Please comment about it for others!!

This site uses Akismet to reduce spam. Learn how your comment data is processed.