Use BorderLayout

The visual editor provides visual cues for helping you work with the Swing BorderLayout.

The BorderLayout manager lays out components into regions defined by compass directions. The class java.awt.BorderLayout implements LayoutManager2, and its constraint is a string that can be of the value "North", "South", "Center", "East", or "West".

Note: If you switch a container to BorderLayout and it has more than five components, only the first five components are added with constraints to the BorderLayout. The remaining components will be moved from the container to the free form area of the Design view.

BorderLayout positions each component at one of the compass constraints along the edge with its preferred width or height, and the "Center" component occupies all of the remaining space.

The constraints value is used as the second argument to the method add(Component,Object) that adds the components to their parent container. For example, the code to initialize a JPanel might look like this:

private void initialize() { 
        
       this.setLayout(new java.awt.BorderLayout()); 
       this.add(getJLabel(), java.awt.BorderLayout.NORTH); 
       this.add(getJScrollBar(), java.awt.BorderLayout.WEST); 
       this.add(getJButton(), java.awt.BorderLayout.EAST);  
       this.setSize(193, 124); 
 }

Note: Two additional constants are used to support relative positioning based on the container's ComponentOrientation: "before line begins" and "after line ends". For example, in a container where ComponenetOrientation is ComponentOrientation.LEFT_TO_RIGHT, "Before line begins" maps to "West", and "After line ends" maps to "East". Mixing the two types of constants is not recommended. Unusual results may show in the graph viewer and at run time, since the relative constants will take precedence.

 

Parent topic

Swing and AWT layout managers

 

Related concepts

Layout managers and containers

 

Related tasks

Using BoxLayout
Using CardLayout
Using FlowLayout
Using GridLayout (AWT)
Using GridBag layout