1. Technology

BorderLayout Example Program

By

Java Code:

Here is example Java code to show how the section contained with the BorderLayout layout manager.

The program will show a window with options for the size of the horizontal and vertical spacing of a BorderLayout. When the "Change Layout" button is pressed the five different sections of the BorderLayout are shown with the assigned spacing.

The article relating to this program is Using the BorderLayout.

Note: To run the Java code this program should be saved in a file called BorderLayoutExample.java

//Imports are listed in full to show what's being used
//could just import javax.swing.* and java.awt.* etc..
import java.awt.EventQueue;
import java.awt.BorderLayout;
import java.awt.Color;
import javax.swing.BorderFactory;
import javax.swing.border.Border;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JComboBox;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Container;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class BorderLayoutExample {

    JFrame guiFrame;
    JComboBox hGap;
    JComboBox vGap;
            
    //Note: Typically the main method will be in a
    //separate class. As this is a simple one class
    //example it's all in the one class.
    public static void main(String[] args) {
     
         //Use the event dispatch thread for Swing components
         EventQueue.invokeLater(new Runnable()
         {
             
            @Override
             public void run()
             {
                 
                 new BorderLayoutExample();         
             }
         });
              
    }
    
    public BorderLayoutExample()
    {
        guiFrame = new JFrame();
        
        //make sure the program exits when the frame closes
        guiFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        guiFrame.setTitle("FlowLayout Example");
        guiFrame.setSize(700,300);
      
        //This will center the JFrame in the middle of the screen
        guiFrame.setLocationRelativeTo(null);
        
        JPanel optionPanel = new JPanel();
        
        JButton changeLayout = new JButton("Change Layout");
        changeLayout.setActionCommand("Change Layout");
        changeLayout.addActionListener(new ActionListener()
        {
            @Override
            public void actionPerformed(ActionEvent event)
            {
               //When the "Change Layout" button is pressed
               //add a JPanel with the chosen BorderLayout settings
               AddJPanel(guiFrame,(Integer)hGap.getSelectedItem(),
                    (Integer)vGap.getSelectedItem());

            }
        });
        
        Integer[] options = {0,5,10,15,20,30};
        hGap = new JComboBox(options);
        hGap.setSelectedIndex(0);
        
        vGap = new JComboBox(options);
        vGap.setSelectedIndex(0);
        
        JLabel horizontalGap = new JLabel("Horizontal Gap:");
        JLabel verticalGap = new JLabel("Vertical Gap:");
        
        optionPanel.add(horizontalGap);
        optionPanel.add(hGap);
        optionPanel.add(verticalGap);
        optionPanel.add(vGap);
        optionPanel.add(changeLayout);
        guiFrame.add(optionPanel, BorderLayout.NORTH);
 
        guiFrame.setVisible(true);
    }
    
    public void AddJPanel(Container parent, int horzSpacing, int vertSpacing)
    {
        
        //create a JPanel and assign it a BorderLayout with
        //the passed values for horiztonal and vertical spacing
        JPanel borderPanel = new JPanel();        
        borderPanel.setLayout(new BorderLayout(horzSpacing, vertSpacing));
        
        //create some labels to show which sections are which
        Border outline = BorderFactory.createLineBorder(Color.black);
        JLabel northLabel = new JLabel("NORTH");
        northLabel.setHorizontalAlignment(SwingConstants.CENTER);
        northLabel.setBorder(outline);
        JLabel southLabel = new JLabel("SOUTH");
        southLabel.setHorizontalAlignment(SwingConstants.CENTER);
        southLabel.setBorder(outline);
        JLabel eastLabel = new JLabel("EAST");
        eastLabel.setHorizontalAlignment(SwingConstants.CENTER);
        eastLabel.setBorder(outline);
        JLabel westLabel = new JLabel("WEST");
        westLabel.setHorizontalAlignment(SwingConstants.CENTER);
        westLabel.setBorder(outline);
        JLabel centerLabel = new JLabel("CENTER");
        centerLabel.setHorizontalAlignment(SwingConstants.CENTER);
        centerLabel.setBorder(outline);
        
        borderPanel.add(northLabel, BorderLayout.NORTH);
        borderPanel.add(southLabel, BorderLayout.SOUTH);
        borderPanel.add(eastLabel, BorderLayout.EAST);
        borderPanel.add(westLabel, BorderLayout.WEST);
        borderPanel.add(centerLabel, BorderLayout.CENTER);
        
        //adding a component to a position will displace any
        //components that are already there
        parent.add(borderPanel,BorderLayout.CENTER);
        parent.invalidate();        
        parent.validate();        
    }
}
  1. About.com
  2. Technology
  3. Java
  4. Create User Interfaces
  5. Layout Managers
  6. BorderLayout Example Program

©2014 About.com. All rights reserved.