Swing Layouts

A list of the layout managers

    *    Flow Layout:    It is the default layout of the managers for the panels. It lays out the components one after another until it runs out of room, then it starts a new row.

    *    Grid Layout:    It's good for creating a grid of identically sized components.

    *    Border Layout:    It's the default layout for the frames. It divides the container into five regions. The top region is called NORTH, bottom region is called SOUTH,
                                    center region is called
CENTER, right region is called EAST, and left region is called WEST

    *    Box Layout:    It arranges the components into either a single column or a single row.

    *    GridBag Layout:    Each of the rows or columns can be a different size, the component can span two or more rows or columns. This layout is so flexible that we can
                                      tell the layout manager what to do if the component is smaller or larger than the space allotted for it.

    *    Group Layout:    It describe an arrangement from two points of view, from left to right and from top to bottom.

In addition the above layouts, java provides few additional one, though the above six layouts are used most often.

Since all the GUIs we've done so far are flow layout, which is the basic layout. We will start with the flow layout.

Flow Layout Manager

Flow layouts often used for small panels that consists of a few components, such as few rows of buttons. Lets see a basic example of it.

Note:    For all the Layout Manager constructors and Methods look at the link below " Swing Method & Constructor Table."

ClickHere to download FlowLayoutButton.java

Border Layout Manager

Now, lets look at one of my favorite layout, the Border Layout. If a border layout is not specify with a region when components are added, then the component is placed in the center region by default. The border layout automatically resizes the components in each region to completely fill up the region. Sometimes, we don't want that to happen, hence we can place the components into a separate panels using flow layout and then add the panel into the border layout region.

ClickHere to download BorderRegion.java
 

Grid Layout Manager

It's designed for panels that need to have a set number of components all equally sized and arranged into a grid. It most often used to create calculator or a phone.

 


ClickHere to download GridLayout.java

 

Box Layout

A box layout is most often use to create panels that contain a single row or column of components. If the components are arranged in a single row, we use the Horizontal method. If the components are stacked in a column, we use the Vertical method.

 

ClickHere to download BoxHorizontal.java

 

 

ClickHere to download BoxVertical.java

 

Click here to continue

 

 

  <Java 24><Home><Swing Method & Constructor Table>