Layouts help you to arrange swing components in your applications .Considered as the simplest Layout to implement in Java, FlowLayout is all about adding swing/awt components one after the another in rows of your windows, panels ….etc. This layout tries to fit as much as components it can in a single row (depending upon the size of your frame/window/parent component) before moving to the next row (If a component can’t be added to the first row 2nd row would be used …and so on ).Adding components successively doesn’t mean that it won’t spare any space between components. A Small horizontal gap is placed between each and every component

The FlowLayout Class has three constructors as follows:-
  1. FlowLayout(int alignment)
  2. FlowLayout(int alignment,int horizontal_gap,int vertical_gap)
  3. FlowLayout()

FlowLayout Java Swing Tutorial:-

In which alignments are of three types :-
  • FlowLayout.CENTER
  • FLOWLAYOUT.RIGHT
  • FLOWLAYOUT.LEFT

 

If you don’t wish to use any of the first two constructors , Java provides you with methods like

  • setAlignment(int Alignment );
  • setHgap(int gap)
  • setVgap(int gap)

to set the Alignments, Vertical Gaps and the Horizontal gaps respectively

Lets look at an example application(below Java Code) that uses the FlowLayout :-
package layouts;
import java.awt.FlowLayout;
import javax.swing.*;
/**
*
* @author Pramod Choudhari | http://techwayz.com
*/
public class FlowLayoutDemo {
public static void main(String[] args) {
JFrame jf = new JFrame();
FlowLayout fl = new FlowLayout();
jf.setTitle(“Flow Layout example”);
jf.setSize(300, 200);
jf.setLocation(300, 200);
jf.setResizable(false);
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
fl.setVgap(10);
jf.add(new JLabel(“Name”));
jf.add(new JTextField(20));
jf.add(new JLabel(“Addrs”));
jf.add(new JTextField(20));
jf.add(new JLabel(“Sex ”));
jf.add(new JComboBox(new Object[]{“Male”, “Female”}));
jf.add(new JLabel(“Blood Group”));
jf.add(new JComboBox(new Object[]{“O+”, “AB+”}));
jf.add(new JButton(“Add”));
jf.add(new JButton(“Update”));
jf.setLayout(fl);
jf.setVisible(true);
}
}
Screenshot (When the above code is run):-

 

Description of the code :-
JFrame jf = new JFrame() :-This line would create and initialize the JFrame object jf.
FlowLayout fl = new FlowLayout():-This line would create and initialize the FlowLayout object fl.
jf.setTitle(“Flow Layout example”):-This line sets the frames title as “Flow Layout example”.
jf.setSize(300, 200):-This line would set the width of our JFrame to 300 and height of our JFrame to 200.
jf.setResizable(false):-This line would disable the default maximizable behavior of our JFrame which is set true when you don’t make it to false.
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE):-This line ensures that window is terminated and the consumed memory space of our application is freed for other applications to use.
fl.setVgap(10):-This line sets the vertical gap/space between the rows in which components sare placed to 10 pixels .
jf.add(new JLabel(“Name”)):-This line creates a JLabel named “Name” and add’s it to our window.
jf.add(new JTextField(20)):-This line creates a JTextField having width 20 pixels and add’s it to our window.
jf.setLayout(fl):-This line is very important , we are passing our fl (FlowLayout) object as a parameter to the LayoutManager which is responsible for arranging the components in your window . If you don’t use this line Default Layout Manager would be used .
jf.setVisible(true):-Without including This line you won’t be able to see your window . Hence This line is a must use line for every swing applications .
the author

I am a post graduate in computer sciences .I have 1+ year experience of working as a Java/J2EE developer in a top notch IT company. I love developing applications and writing technology related articles .