The BorderLayout Manager
The BorderLayout manager divides a container into five regions. The regions are north, east, south, west and center. The BorderLayout is a default layout for window objects suchs the JFrame, JWindow, JDialog, JInternalFrame etc.
Template of the BorderLayout Manager
When you add a component to a container that is managed by a Border layout, the component has to be placed in one of the five regions of the layout. You can place only one component at each of the five regions.
Constructors of the BorderLayout
(1) BorderLayout(): This constructor constructs a border layout with no gaps between the components.
(2) BorderLayout(int horizontal gap, int vertical gap) : This constructs a border layout with the specified gaps between the components.
Commonly Used Methods
(1) getLayoutAlignmentX(Container parent) : Returns the layout alignment along the X axis
(2) getLayoutAlignmentY(Container parent) : returns the layout alignment along the Y axis
(3) removeLayoutComponent(Component comp) : removes a specified component from the boderlayout
(4) getHgap() : returns the horizontal gap between the components
(5) getVgap() : returns the vertical gap between the components
(6) setHgap(int hgap) : sets the horizontal gap between the components
(7) setVgap(int vgap) : sets the vertical gap between the components
Rules of the BorderLayout
(1) Each region can hold only one component
(2) When a component is added to a region, it is stretched to fill the entire region
Program to demonstrate how components are added to a Border layout
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 |
package villagecoder; import java.awt.BorderLayout; //needed for the BorderLayout class import javax.swing.JButton; //needed for the JButton class import javax.swing.JFrame; //needed for the JFrame class public class BorderWindow extends JFrame { private JButton button1; private JButton button2; private JButton button3; private JButton button4; private JButton button5; private final int WINDOW_WIDTH=400; private final int WINDOW_HEIGHT=300; public BorderWindow(){ //set the Title bar setTitle("Border Layout"); //specify an action for the close button setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the size of the window setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //create five buttons button1=new JButton("Button 1"); button2=new JButton("Button 2"); button3=new JButton("Button 3"); button4=new JButton("Button 4"); button5=new JButton("Button 5"); //add the buttons to the five regions add(button1, BorderLayout.NORTH); //add button1 to the north or top region add(button2, BorderLayout.WEST); //add button2 to the west or left region add(button3, BorderLayout.SOUTH); //add button3 to the south or bottom region add(button4, BorderLayout.EAST); //add button4 to the east or right region add(button5, BorderLayout.CENTER); //add button5 to the center or middle region //show the window setVisible(true); } /* * main window */ public static void main(String[] args) { //create instance of the BorderWindow new BorderWindow(); } } |
Output
When you don’t specify a region for a component
If you add a component to a Border layout container without specifying the region, the component is by default added to the center region. In other words, the statement
1 |
setLayout(new BorderLayout()) |
is equivalent to
1 |
setLayout(new BorderLayout(BorderLayout.CENTER)) |
If you add more components without specifying the region, the components are stacked on one another at the center region and the window will show only the latest component added.
Program to demonstrate that adding components without specifying region stacks them at the center region
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 57 58 59 60 |
package villagecoder; import java.awt.BorderLayout; //needed for the BorderLayout class import javax.swing.JButton; //needed for the JButton class import javax.swing.JFrame; //needed for the JFrame class /*Program demonstrates that when regions are not specified in a border layout, components are added at the center region one on top of the another *with only the last added component visible */ public class BorderWindow extends JFrame { private JButton button1; private JButton button2; private JButton button3; private JButton button4; private JButton button5; private final int WINDOW_WIDTH=400; private final int WINDOW_HEIGHT=300; public BorderWindow(){ //set the Title bar setTitle("Border Layout"); //specify an action for the close button setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the size of the window setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //create five buttons button1=new JButton("Button 1"); button2=new JButton("Button 2"); button3=new JButton("Button 3"); button4=new JButton("Button 4"); button5=new JButton("Button 5"); //add the buttons to the center region. NOTE: Only the button5 shows because it is the last or most current to be added add(button1); //add button1 to the center region add(button2); //add button2 to the center region add(button3); //add button3 to the center region add(button4); //add button4 to the center region add(button5); //add button5 to the center region //show the window setVisible(true); } /* * main window */ public static void main(String[] args) { //create instance of the BorderWindow new BorderWindow(); } } |
Output
Rules of the BorderLayout Manager
- A component placed in either the north or south region can be resized horizontally to fill the entire region. The north and south regions are vertically aligned and they can be resized horizontally.
- A component placed in either the east or west regions can be resized vertically so that it fills up the entire region.
- A component placed in the center region may be resized both horizontally and vertically so it fills up the entire region.
Horizontal and Vertical gaps between components in a border layout
The regions in a BorderLayout object do not have any gaps between them. The horizontal gap can be set using the setHgap(int hgap) method. The vertical gap can be set using the setVgap(int vgap) method.
Alternatively, you can use an overloaded version of the BorderLayout constructor to specify horizontal and vertical gaps.
1 |
BorderLayout(int horizontal gap,int vertical gap); |
Example:
1 |
setLayout(new BorderLayout(5,10)); |
//border layout is created with 5 pixels horizontally and 10 pixels vertically
Alternative names to describe position of region in border layout
BorderLayout allows the use of relative positioning constants to define the position of components.
Region
- NORTH
- SOUTH
- WEST
- EAST
- CENTER
Alternative Name 1
- PAGE_START
- PAGE_END
- LINE_START
- LINE_END
- CENTER
Alternative Name 2
- BEFORE_FIRST_LINE
- AFTER_LAST_LINE
- BEFORE_LINE_BEGINS
- AFTER_LINE_ENDS
- CENTER
Note: Do not mix the absolute positioning constants with the relative positioning constants
Program to demonstrate the use of PAGE_START, PAGE_END, LINE_START, LINE_END
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 |
package villagecoder; import java.awt.BorderLayout; //needed for the BorderLayout class import javax.swing.JButton; //needed for the JButton class import javax.swing.JFrame; //needed for the JFrame class public class BorderWindow extends JFrame { private JButton button1; private JButton button2; private JButton button3; private JButton button4; private JButton button5; private final int WINDOW_WIDTH=400; private final int WINDOW_HEIGHT=300; public BorderWindow(){ //set the Title bar setTitle("Border Layout"); //specify an action for the close button setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the size of the window setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //create five buttons button1=new JButton("Button 1"); button2=new JButton("Button 2"); button3=new JButton("Button 3"); button4=new JButton("Button 4"); button5=new JButton("Button 5"); //add the buttons to the five regions add(button1, BorderLayout.PAGE_START); //add button1 to the north or top region add(button2, BorderLayout.LINE_START); //add button2 to the west or left region add(button3, BorderLayout.PAGE_END); //add button3 to the south or bottom region add(button4, BorderLayout.LINE_END); //add button4 to the east or right region add(button5, BorderLayout.CENTER); //add button5 to the center or middle region //show the window setVisible(true); } /* * main window */ public static void main(String[] args) { //create instance of the BorderWindow new BorderWindow(); } } |
Output
Program to demonstrate the use of the BEFORE_FIRST_LINE, AFTER_LAST_LINE, BEFORE_LINE_BEGINS, AFTER_LINE_ENDS
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 |
package villagecoder; import java.awt.BorderLayout; //needed for the BorderLayout class import javax.swing.JButton; //needed for the JButton class import javax.swing.JFrame; //needed for the JFrame class public class BorderWindow extends JFrame { private JButton button1; private JButton button2; private JButton button3; private JButton button4; private JButton button5; private final int WINDOW_WIDTH=400; private final int WINDOW_HEIGHT=300; public BorderWindow(){ //set the Title bar setTitle("Border Layout"); //specify an action for the close button setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //set the size of the window setSize(WINDOW_WIDTH, WINDOW_HEIGHT); //create five buttons button1=new JButton("Button 1"); button2=new JButton("Button 2"); button3=new JButton("Button 3"); button4=new JButton("Button 4"); button5=new JButton("Button 5"); //add the buttons to the five regions add(button1, BorderLayout.BEFORE_FIRST_LINE); //add button1 to the north or top region add(button2, BorderLayout.BEFORE_LINE_BEGINS); //add button2 to the west or left region add(button3, BorderLayout.AFTER_LINE_ENDS); //add button3 to the south or bottom region add(button4, BorderLayout.AFTER_FIRST_LINE); //add button4 to the east or right region add(button5, BorderLayout.CENTER); //add button5 to the center or middle region //show the window setVisible(true); } /* * main window */ public static void main(String[] args) { //create instance of the BorderWindow new BorderWindow(); } } |
Recent Comments