FlowLayout alignment - Java Swing

Java examples for Swing:FlowLayout

Introduction

By default, a FlowLayout aligns components in the center of the container.

To change the alignment, use its setAlignment() method or pass the alignment in its constructor.

import java.awt.FlowLayout;

public class Main {
  public static void main(String[] argv) throws Exception {
    // Set the alignment when you create the layout manager object
    FlowLayout flowLayout = new FlowLayout(FlowLayout.RIGHT);

    // Set the alignment after you have created the flow layout manager
    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 depending 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.
  • 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.

Related Tutorials