Java Swing Tutorial - Java Swing FlowLayout








The FlowLayout lays out the components in the order they are added to the container horizontally, and then vertically.

When it is positioning the components horizontally, it may lay them left to right, or right to left.

The horizontal layout direction depends on the orientation of the container. We can set the orientation of a container by calling its setComponentOrientation() method.

To set the orientation of a container and all its children, use the applyComponentOrientation() method instead.

The following code shows how to set the orientation of the content pane of a frame to "right to left".

JFrame  frame  = new JFrame("Test");
Container pane  = frame.getContentPane();
pane.setComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

Here is another way to do the same thing as above.

JFrame  frame  = new JFrame("Test");
Container pane  = frame.getContentPane();
pane.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

To support locale and orientation in a more generic way, globally set the default locale for all Swing components:

// "ar" is used  for Arabic   locale
JComponent.setDefaultLocale(new Locale("ar"));

When we create a JFrame, we can get the component's orientation according to the default locale and set it to the frame and its children.

This way, we do not have to set the orientation for every container in your application.

Locale   defaultLocale = JComponent.getDefaultLocale();
ComponentOrientation componentOrientation  = ComponentOrientation.getOrientation(defaultLocale);
frame.applyComponentOrientation(componentOrientation);

A FlowLayout tries to place all components into one row, giving them their preferred size. If all components do not fit into one row, it starts another row.

FlowLayout can add extra space to the width and height to account for horizontal and vertical gaps between the components.

import java.awt.Container;
import java.awt.FlowLayout;
// w  w  w .  j  ava 2s .c o m
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Layout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    Container contentPane = frame.getContentPane();
    contentPane.setLayout(new FlowLayout());

    for (int i = 1; i <= 5; i++) {
      contentPane.add(new JButton("Button  " + i));
    }

    frame.pack();
    frame.setVisible(true);
  }
}




Alignment

By default, a FlowLayout aligns all components in the center of the container. We can change the alignment by calling its setAlignment() method or passing the alignment in its constructor, like so:

To set the alignment when we create the layout manager object

FlowLayout  flowLayout = new FlowLayout(FlowLayout.RIGHT);

To set the alignment after we have created the flow layout manager

FlowLayout  flowLayout = new FlowLayout(FlowLayout.LEFT);
flowLayout.setAlignment(FlowLayout.RIGHT);

The following five constants are defined in the FlowLayout class to represent the five different alignments:

  • LEFT
  • RIGHT
  • CENTER
  • LEADING
  • TRAILING

The LEADING alignment may mean either left or right, it depends on the orientation of the component.

If the component's orientation is RIGHT_TO_LEFT, the LEADING alignment means RIGHT. If component's orientation is LEFT_TO_RIGHT, the LEADING alignment means LEFT.

TRAILING alignment may mean either left or right. If the component's orientation is RIGHT_TO_LEFT, the TRAILING alignment means LEFT. If component's orientation is LEFT_TO_RIGHT, the TRAILING alignment means RIGHT.

We can set the gaps between two components either in the constructor of the FlowLayout class or using its setHgap() and setVgap() methods.

import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;
// w  w w .  j  av a 2 s. c o m
import javax.swing.JButton;
import javax.swing.JFrame;

public class Main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("Layout");
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    int horizontalGap = 20;
    int verticalGap = 10;
    Container contentPane = frame.getContentPane();
    FlowLayout flowLayout = new FlowLayout(FlowLayout.LEADING, horizontalGap,
        verticalGap);
    contentPane.setLayout(flowLayout);
    frame.applyComponentOrientation(ComponentOrientation.RIGHT_TO_LEFT);

    for (int i = 1; i <= 5; i++) {
      contentPane.add(new JButton("Button  " + i));
    }
    frame.pack();
    frame.setVisible(true);
  }
}