0% found this document useful (0 votes)
87 views7 pages

Layout Managers

The document discusses layout managers in Java Swing. It explains that a layout manager determines the size and position of components within a container. The default layout managers are FlowLayout for JPanels and BorderLayout for content panes. Developers can also set custom layout managers using the setLayout() method. It then provides details about four specific layout managers: BorderLayout, FlowLayout, GridLayout, and BoxLayout.

Uploaded by

danniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views7 pages

Layout Managers

The document discusses layout managers in Java Swing. It explains that a layout manager determines the size and position of components within a container. The default layout managers are FlowLayout for JPanels and BorderLayout for content panes. Developers can also set custom layout managers using the setLayout() method. It then provides details about four specific layout managers: BorderLayout, FlowLayout, GridLayout, and BoxLayout.

Uploaded by

danniel
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Layout Managers

A layout manager is an object that implements the Layout Manager interface* and
determines the size and position of the components within a container. Although
components can provide size and alignment hints, a container's layout manager
has the final say on the size and position of the components within the container.
The layout manager's job is to fit the components into the available area, while
maintaining the proper spatial relationships between the components.
Every container has a default layout manager; therefore, when you make a new
container, it comes with a LayoutManager object of the appropriate type.
Each JPanel object is initialized to use a FlowLayout, unless you specify differently
when creating the JPanel. Content panes use BorderLayout by default. If you do
not like the default layout manager that a panel or content pane uses, you are
free to change it to a different one
You can install a new layout manager at any time with the setLayout() method.
Below, we set the layout manager of a container to a BorderLayout:
setLayout ( new BorderLayout() );
1. BorderLayout
2. FlowLayout
3. GridLayout
4. BoxLayout

1.BorderLayout
The BorderLayout divides the container into five areas which
include: PAGE_START, PAGE_END, LINE_START, CENTER and LINE_END.
You can use the constants of BorderLayout class to indicate the area where you
want to place a component. If you enlarge the window, you can see that the
center area take up as much of space as possible and the other areas only expand
to fill available space.
You can set the horizontal and vertical gap between components by using
methods setHgap() and setVgap().
The BorderLayout is look like as

import java.awt.*;
import javax.swing.*;

public class Main {


public static void main(String[] args) {

JFrame frame = new JFrame("BorderLayout Demo");


JButton btn1 = new JButton("Button 1 (PAGE_START)");
JButton btn2 = new JButton("Button 2 (CENTER)");
JButton btn3 = new JButton("Button 3 (LINE_START)");
JButton btn4 = new JButton("Button 4 (PAGE_END)");
JButton btn5 = new JButton("Button 5 (LINE_END)");

JPanel panel = new JPanel(new BorderLayout());


panel.add(btn1, BorderLayout.PAGE_START);
panel.add(btn2, BorderLayout.CENTER);
panel.add(btn3, BorderLayout.LINE_START);
panel.add(btn4, BorderLayout.PAGE_END);
panel.add(btn5, BorderLayout.LINE_END);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(500,200);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}
2.FlowLayout
FlowLayout is the simplest layout in Java Swing layouts. The FlowLayout places
components from left to right in a row using preferred component sizes until no
space is available in the container. When no space is available, a new row is
started. The placement of the component depends on the size of the container
therefore you cannot guarantee which row the component is placed.
The FlowLayout is useful as a pad for a single component to make sure that the
component will be placed at the center area of a container. Notice that
FlowLayout is default layout of JPanel container.
The FlowLayout application is look like as

import java.awt.*;
import javax.swing.*;

public class Main {


public static void main(String[] args) {

JFrame frame = new JFrame("FlowLayout Demo");


JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JButton btn3 = new JButton("Button 3");
JButton btn4 = new JButton("Button 4");
JButton btn5 = new JButton("Button 5");
// FlowLayout is default for JPanel
JPanel panel = new JPanel(new FlowLayout());
// add buttons to the panel
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
panel.add(btn5);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,150);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}

3.GridLayout
GridLayout places components in a cell of a grid. Each cell has the same size
therefore each component takes up the same space in a container. When user
adjusts the container, the size of each component changes accordingly.
Here are the constructors of the GridLayout class:
Constructors Description
public GridLayout(int rows, int cols) Creates a grid layout with a given number
of rows and columns. If cols or rows is zero,
Constructors Description
any numbers of components can be placed
in a column or in a row.
Creates a grid layout with a given number
public GridLayout(int rows, int cols, int of rows and columns. Beside this, you can
hgap, int vgap) initialize the vertical and horizontal gap
between each of rows and columns.
The GridLayout application is look like as

import java.awt.*;
import javax.swing.*;

public class Main {


public static void main(String[] args) {

JFrame frame = new JFrame("GridLayout Demo");


JButton btn1 = new JButton("Button 1");
JButton btn2 = new JButton("Button 2");
JButton btn3 = new JButton("Button 3");
JButton btn4 = new JButton("Button 4");
JButton btn5 = new JButton("Button 5");
// create grid layout with 3 rows , 2 columns with horizontal
// and vertical gap set to 10
JPanel panel = new JPanel(new GridLayout(3,2,10,10));
// add buttons to the panel
panel.add(btn1);
panel.add(btn2);
panel.add(btn3);
panel.add(btn4);
panel.add(btn5);

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,150);
frame.getContentPane().add(panel);
frame.setVisible(true);
}
}

4.BoxLayout
The BoxLayout is used to arrange the components either vertically or horizontally.
1. Box class offers a container that uses BoxLayout as its default layout
manager.
2. BoxLayout works to honor each component's x and y alignment properties
as well as its maximum size.
The BoxLayout class has only one constructor
public BoxLayout(java.awt.Container target, int axis)
1. 'target' argument specifies the container that needs to be laid out.
2. 'axis' specifies the axis to lay out components along.
The value of axis can be one of the following:
1. BoxLayout.X_AXIS
2. BoxLayout.Y_AXIS
3. BoxLayout.LINE_AXIS
4. BoxLayout.PAGE_AXIS
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;

public class BoxLayoutTest {

public static void main(String[] args) {


JFrame.setDefaultLookAndFeelDecorated(true);
JFrame frame = new JFrame("BoxLayout Test");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
BoxLayout boxLayout = new BoxLayout(frame.getContentPane(),
BoxLayout.Y_AXIS); // top to bottom
frame.setLayout(boxLayout);
frame.add(new JButton("Button 1"));
frame.add(new JButton("Button 2"));
frame.add(new JButton("Button 3"));
frame.pack();

frame.setVisible(true);
}
}

Output:

You might also like