FlowLayout



The following example shows how to set the FlowDirection and WrapContents properties on a FlowLayoutPanel control. Paste the code into the Form1 source file. If your project contains a file named Form1.Designer.cs or Form1.Designer.vb, remove that file from the project. Using the Flow Layout. You can arrange items in your collection views using a concrete layout object, the UICollectionViewFlowLayout class. The flow layout implements a line-based breaking layout, which means that the layout object places cells on a linear path and fits as many cells along that line as it can. FlowLayout is an opensource Android library that alows developers to easily integrate flow layout into their app. FlowLayout is an layout that display its children in multiple rows depending on their size. Source code with examples is included in repository. Dependencies Gradle compile 'com.wefika:flowlayout:' Maven. 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.


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'.

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

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

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.

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.

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

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

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.


Note: This lesson covers writing layout code by hand, which can be challenging. If you are not interested in learning all the details of layout management, you might prefer to use the GroupLayout layout manager combined with a builder tool to lay out your GUI. One such builder tool is the NetBeans IDE. Otherwise, if you want to code by hand and do not want to use GroupLayout, then GridBagLayout is recommended as the next most flexible and powerful layout manager.

If you are interested in using JavaFX to create your GUI, seeWorking With Layouts in JavaFX.

The FlowLayout class provides a very simple layout manager that is used, by default, by the JPanel objects. The following figure represents a snapshot of an application that uses the flow layout:

Click the Launch button to run FlowLayoutDemo using Java™ Web Start (download JDK 7 or later). Alternatively, to compile and run the example yourself, consult the example index.


The complete code of this demo is in the FlowLayoutDemo.java file.

The FlowLayout class puts components in a row, sized at their preferred size. If the horizontal space in the container is too small to put all the components in one row, the FlowLayout class uses multiple rows. If the container is wider than necessary for a row of components, the row is, by default, centered horizontally within the container. To specify that the row is to aligned either to the left or right, use a FlowLayout constructor that takes an alignment argument. Another constructor of the FlowLayout class specifies how much vertical or horizontal padding is put around the components.

The code snippet below creates a FlowLayout object and the components it manages.

Select either the Left to Right or Right to Left option and click the Apply orientation button to set up the component's orientation. The following code snippet applies the Left to Right components orientation to the experimentLayout.

The FlowLayout API

Swing Flowlayout Vertical

The following table lists constructors of the FlowLayout class.

Flowlayoutpanel C#

ConstructorPurpose
FlowLayout()Constructs a new FlowLayout object with a centered alignment and horizontal and vertical gaps with the default size of 5 pixels.
FlowLayout(int align)Creates a new flow layout manager with the indicated alignment and horizontal and vertical gaps with the default size of 5 pixels. The alignment argument can be FlowLayout.LEADING, FlowLayout.CENTER, or FlowLayout.TRAILING. When the FlowLayout object controls a container with a left-to right component orientation (the default), the LEADING value specifies the components to be left-aligned and the TRAILING value specifies the components to be right-aligned.
FlowLayout (int align, int hgap, int vgap)Creates a new flow layout manager with the indicated alignment and the indicated horizontal and vertical gaps. The hgap and vgap arguments specify the number of pixels to put between components.

Examples that Use FlowLayout

The following table lists code examples that use the FlowLayout class and provides links to related sections.

ExampleWhere DescribedNotes
FlowLayoutDemoThis pageSets up a content pane to use FlowLayout. If you set the RIGHT_TO_LEFT constant to true and recompile, you can see how FlowLayout handles a container that has a right-to-left component orientation.
CardLayoutDemoHow to Use CardLayoutCenters a component nicely in the top part of a BorderLayout, and puts the component in a JPanel that uses a FlowLayout.
ButtonDemoHow to Use Buttons, Check Boxes, and Radio ButtonsUses the default FlowLayout of a JPanel.
TextInputDemoHow to Use Formatted Text FieldsUses a panel with a right-aligned FlowLayout presenting two buttons.