FlowLayout: the default layout manager for a JPanel. : FlowLayout « Swing « Java Tutorial






  1. A FlowLayout adds components to the container in rows.
  2. When it can't fit more components in a row, it starts a new row.
  3. Components within a FlowLayout-managed container are given their preferred size.
  4. If there is insufficient space, you do not see all the components.

There are three constructors for creating the FlowLayout layout manager:

public FlowLayout()
public FlowLayout(int alignment)
public FlowLayout(int alignment, int hgap, int vgap)

If an alignment is not specified, default is centered. The setting is controlled by one of the following constants:

  1. FlowLayout.LEFT. Left-justify component rows.
  2. FlowLayout.RIGHT. Right-justify component rows.
  3. FlowLayout.CENTER. Center component rows.
  4. FlowLayout.LEADING. Justify component rows to the leading edge of the container's orientation, e.g. to the left in the left-to-right orientation.
  5. FlowLayout.TRAILING. Justify component rows to the trailing edge of the container's orientation, e.g. to the right in the left-to-right orientation.
  1. You can specify the gaps, in pixels, both horizontal (hgap) and vertical (vgap).
  2. The horizontalGap argument determines the distance between two components in the same row and between the components and the container border.
  3. The verticalGap argument determines the distance between components in adjacent rows and the components and the container border.
  4. The default for both horizontalGap and verticalGap is 5 unit.
  5. Negative gaps place component on top of one another.
import java.awt.Component;
import java.awt.ComponentOrientation;
import java.awt.Container;
import java.awt.FlowLayout;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JMenu;

public class FlowLayoutComponentOrientationRIGHT_TO_LEFT extends JFrame {

  public FlowLayoutComponentOrientationRIGHT_TO_LEFT() {
    getContentPane().setLayout(new FlowLayout());
    getContentPane().add(new JButton("OK"));
    getContentPane().add(new JButton("Cancel"));

    applyOrientation(this, ComponentOrientation.RIGHT_TO_LEFT);
  }

  public static void main(String[] argv) {
    JFrame frame = new FlowLayoutComponentOrientationRIGHT_TO_LEFT();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.pack();
    frame.setVisible(true);
  }

  private void applyOrientation(Component c, ComponentOrientation o) {
    c.setComponentOrientation(o);

    if (c instanceof JMenu) {
      JMenu menu = (JMenu) c;
      int ncomponents = menu.getMenuComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(menu.getMenuComponent(i), o);
      }
    } else if (c instanceof Container) {
      Container container = (Container) c;
      int ncomponents = container.getComponentCount();
      for (int i = 0; i < ncomponents; ++i) {
        applyOrientation(container.getComponent(i), o);
      }
    }
  }
}








14.89.FlowLayout
14.89.1.Laying Out Components in a Flow (Left-to-Right, Top-to-Bottom)
14.89.2.Three constructors available for the FlowLayout manager.
14.89.3.Layout manager
14.89.4.FlowLayout: the default layout manager for a JPanel.
14.89.5.FlowLayout BehaviorFlowLayout Behavior
14.89.6.Using FlowLayoutUsing FlowLayout
14.89.7.Changing the GapChanging the Gap
14.89.8.Setting the gaps between components and rows explicitly by calling the setHgap()Setting the gaps between components and rows explicitly by calling the setHgap()
14.89.9.Demonstrates how to fix common alignment problemsDemonstrates how to fix common alignment problems
14.89.10.Use FlowLayout to hold checkBox, Label and TextField