Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.4. Layouts  >   5.4.4. How to use BorderLayout

5.4.4. How to use BorderLayout
Last updated: 1 February 2013.

BorderLayout breaks up a container into 5 areas: PAGE_START, PAGE_END, LINE_START, LINE_END and CENTER. In the following sample, a button is placed in each area:

import java.awt.BorderLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public final class BorderLayoutExample extends JFrame {

   
public BorderLayoutExample(){
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addComponents(){
         
JPanel panel = new JPanel();
          panel.setLayout
(new BorderLayout());

          panel.add
(new JButton("PAGE_START"), BorderLayout.PAGE_START);
          panel.add
(new JButton("PAGE_END"), BorderLayout.PAGE_END);
          panel.add
(new JButton("LINE_START"), BorderLayout.LINE_START);
          panel.add
(new JButton("LINE_END"), BorderLayout.LINE_END);
          panel.add
(new JButton("CENTER"), BorderLayout.CENTER);

          add
(panel);
   
}


   
private void init(){
         
setTitle("BorderLayoutExample");
          setSize
(700, 400);
          setLocation
(200, 100);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new BorderLayoutExample();
               
}
          })
;
   
}
}


Here is the result:


Border layout example


As you can see, the area must be specified when you add a component. If the area is not specified, the component is added by default to the center. Thus, the two following statements are the same:

panel.add(new JButton("CENTER"), BorderLayout.CENTER);

panel.add
(new JButton("CENTER"));

Each component takes up all the available space in its area. The center is the core area and it takes up more space than the other areas. An area can contain at most one component. Generally, a JPanel object containing components of its own is added to each area.

Except the center, every other area can be empty and in that case, the center takes up the available extra space. As an example, let's build a basic plain text editor that leaves the areas LINE_START and LINE_END empty. The text editor is organized this way:Here is what it looks like:


Border layout example



The code follows:

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextArea;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.EtchedBorder;

public final class BorderLayoutExample2 extends JFrame {

   
public BorderLayoutExample2(){
         
init();
          addComponents
();

          setDefaultCloseOperation
(JFrame.EXIT_ON_CLOSE);
          setVisible
(true);
   
}


   
private void addButton(String name, Container container, int width, int height){
         
// Create the button
         
JButton button = new JButton(name);

         
// Set the preferred size to the specified value
         
button.setPreferredSize(new Dimension(width, height));
          button.setBorder
(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));

         
// Add the button to the container
         
container.add(button);
   
}


   
private void addComponents(){
         
JPanel mainPanel = new JPanel();
          mainPanel.setLayout
(new BorderLayout());


         
// Setting up the PAGE_START area ---------------------------------------------
          // ------------------------------------------------------------------------------------
         
JPanel pageStartPanel = new JPanel();
         // The layout is left to the default FlowLayout

         
int buttonWidth = 70;
         
int buttonHeight = 25;

          addButton
("Open", pageStartPanel, buttonWidth, buttonHeight);
          addButton
("Save", pageStartPanel, buttonWidth, buttonHeight);
          addButton
("Cut", pageStartPanel, buttonWidth, buttonHeight);
          addButton
("Copy", pageStartPanel, buttonWidth, buttonHeight);
          addButton
("Paste", pageStartPanel, buttonWidth, buttonHeight);
          addButton
("Search", pageStartPanel, buttonWidth, buttonHeight);
          addButton
("Select All", pageStartPanel, buttonWidth, buttonHeight);
          addButton
("Undo", pageStartPanel, buttonWidth, buttonHeight);
          addButton
("Redo", pageStartPanel, buttonWidth, buttonHeight);

          mainPanel.add
(pageStartPanel, BorderLayout.PAGE_START);
         
// ------------------------------------------------------------------------------------
          // ------------------------------------------------------------------------------------


          // Setting up the CENTER area
         
JTextArea editorTextArea = new JTextArea();
          mainPanel.add
(editorTextArea, BorderLayout.CENTER);

         
// Setting up the PAGE_END area
         
JLabel absolutePathLabel = new JLabel();
          absolutePathLabel.setText
("/absolute/path/of/the/currently/open/file/filename.txt");
          mainPanel.add
(absolutePathLabel, BorderLayout.PAGE_END);

          add
(mainPanel);
   
}


   
private void init(){
         
try{
               
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
                // The above statement will be explained in the tutorial 5.6 (Look and Feel)
         
}
         
catch(Exception e){
               
e.printStackTrace();
         
}
         
setTitle("BorderLayoutExample");
          setSize
(700, 400);
          setLocation
(200, 100);
   
}


   
public static void main(String[] args){
         
SwingUtilities.invokeLater(new Runnable() {
               
public void run() {
                     
new BorderLayoutExample2();
               
}
          })
;
   
}
}


Given that the GUI is built without the logic, nothing happens when you click a button. The logic will be provided in the tutorials File choosers (buttons Open and Save) and Text areas (all buttons).


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.4. Layouts  >   5.4.4. How to use BorderLayout
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   5. Graphical User Interfaces  >   5.4. Layouts  >   5.4.5. How to use CardLayout

Copyright © 2013 - 2016. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7