< Prev
Next >



BorderLayout Manager




BorderLayout manager is used to position five components in five different regions in a container. These five regions are north, south, east, west and center, where, each component is played in each of these regions. Each of component positioned at the side has a narrow width, while the component at center part is given the largest space.

Note : Using BorderLayout manager, we can only add one component in a particular region. If we add more than one component in any region, only the last added component will be visible in that region.

The region such as four sides and the center part are referred as constant values, shown in the table -:

BorderLayout constants

Constant Description
BorderLayout.NORTH It is a narrow side at the top, within the container
BorderLayout.SOUTH It is a narrow side at the bottom, within the container
BorderLayout.EAST It is a narrow side at the far right, within the container.
BorderLayout.WEST It is a narrow side at the far right, within the container..
BorderLayout.CENTER It is a large part at the the center of container.



Constructors of BorderLayout

Constructor Description
public BorderLayout() Creates a BorderLayout, with no space between the components
public BorderLayout(int horizontalSpace, int verticalSpace) Creates a Borderlayout, which lets us specify the horizontal and vertical space between the components.



BorderLayout methods

Methods Description
public void add(Component comp, Object region) This method adds the component, comp, to a region in container, where a region is either of the five BorderLayout constant values.
public void setLayout(LayoutManager object) This method sets the layout of the components in a container.

LayoutManager is an interface which is implemented by BorderLayout manager class.


An example display the use of BorderLayout Manager.

  • Finally, JPanel is added to the container, JFrame.

  • import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
    
    public class BorderLayoutEx 
    {
    public static void main(String... ar)
    {
    SwingUtilities.invokeLater(new Runnable() {
    public void run()
    {
    new A();
    }
    });
    
    }//Closing the main method
    }//Closing the class A
    
    
    class A 
    {
    JTextArea textArea;
    JButton button1, button2, button3, button4;
    JFrame jf;
    JPanel jp;
    
    
    A()
    {
    jf = new JFrame("BorderLayout Example");
    jp= new JPanel();
    
    button1 = new JButton("North");
    button2 = new JButton("South");
    button3 = new JButton("East");
    button4 = new JButton("West");
    
    
    textArea = new JTextArea(4,4); //calling TextField(int row, int columns)
    
    
    //Setting the positioning of the components in container, JPanel, to BorderLayout
    jp.setLayout(new BorderLayout()); //
    
    jp.add(button1, BorderLayout.NORTH); 	//Adding button1, to the NORTH, top side 
    jp.add(button2, BorderLayout.SOUTH);	//Adding button2, to the SOUTH, bottom side
    jp.add(button3, BorderLayout.EAST);	//Adding button3, to the EAST, far right side
    jp.add(button4, BorderLayout.WEST);	//Adding button4, to the North, far left side
    jp.add(textArea, BorderLayout.CENTER);	//Adding textarea, to the CENTER
    
     
    
    jf.add(jp);
    
    jf.setSize(320,200);
    jf.setVisible(true); 
    }
    
    }
    
    When you run the code, you are presented a window that contains the components such as four buttons and a blank textarea positioned using BorderLayout manager. As you may see in the figure1, the center area covers the maximun space, while four buttons at the sides have a narrow width.

    Figure 1

    In the figure2, we have typed in the textarea, which was positioned at the center of containter, JFrame.
    Figure 2



    Please share this article -

    Facebook Google Pinterest Reddit Tumblr Twitter




    < Prev
    Next >
    < FlowLayout Manager
    GridLayout Manager >
    Please subscribe for notifications, we post a new article everyday.

    Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

    Coming Next
    -
    JSP & Servlets

    Ad2