Skip to content
APPCITORThe Tech Blog
Esc

Java OOP – Java Swing Applications (Layout Manager)

September 1, 2020 1 min read

Java OOP – Java Swing Applications (Layout Manager)

The mechanism of arranging swing controls by adding controls to a container is called layout manager. There are following classes that represents the layout managers:

  1. java.awt.BorderLayout
  2. java.awt.FlowLayout
  3. java.awt.GridLayout
  4. java.awt.CardLayout
  5. java.awt.GridBagLayout
  6. javax.swing.BoxLayout
  7. javax.swing.GroupLayout
  8. javax.swing.ScrollPaneLayout
  9. javax.swing.SpringLayout etc.

BorderLayout

The BorderLayout is used to arrange the components in five regions: north, south, east, west and center. Each region (area) may contain one component only. Let’s see an example on borderlayout.

Here the default constructor creates a border layout but with no gaps between the components. JBorderLayout(int hgap, int vgap) creates a border layout with the given horizontal and vertical gaps between the components.

FlowLayout

The FlowLayout is used to arrange the components in a line, one after another (in a flow). It is the default layout of applet or panel. Let’s see an example on Flowlayout.

Here default constructor creates a flow layout with centered alignment and a default 5 unit horizontal and vertical gap. FlowLayout(int alignment) creates a flow layout with the given alignment and a default 5 unit horizontal and vertical gap.

GridLayout

The GridLayout is used to arrange the components in rectangular grid. One component is displayed in each rectangle. Let’s see an example on grid layout.

Here default constructor creates a grid layout with one column per component in a row. GridLayout(int rows, int columns) creates a grid layout with the given rows and columns but no gaps between the components.

Let’s meet with the last article of Java OOP on the topic Garbage Collection. See you guys.

Thank you.

Share this article

Keep reading