Java AWT | FlowLayout Last Updated : 11 Jul, 2025 Comments Improve Suggest changes 1 Likes Like Report FlowLayout is used to arrange components in a sequence one after the other. The default layout of applet and panel is FlowLayout. Constructors : FlowLayout(): It will Construct a new FlowLayout with centered alignment.The horizontal and vertical gap will be 5 pixels. FlowLayout(int align) : It will Construct a new FlowLayout with given alignment.The horizontal and vertical gap will be 5 pixels. FlowLayout(int align, int HorizontalGap, int VerticalGap ): It will Construct a new FlowLayout with given alignment, the given horizontal and vertical gap between the components. JLabel(String text): It will create a JLabel instance with the specified text. Commonly used methods: setTitle(String Text): This Method is used to set Title of JFrame. The title you want to set is passed as a string. getAlignment(): Returns the alignment for this layout. setAlignment(int align): used to set the alignment for this layout. removeLayoutComponent(Component comp): Used to remove the component passed as argument from the layout. Below programs will illustrate the Example of FlowLayout in java. Program 1: The following program illustrates the use of FlowLayout by arranging several JLabel components in a JFrame, whose instance class is named as "Example". We create 5 JLabel components named "l1", "l2"... "l5" and then add them to the JFrame by the method this.add(). We set the title and bounds of the frame by method setTitle and setBounds. The layout is set by the method setLayout(); Java // Java program to show Example of FlowLayout. // in java. Importing different Package. import java.awt.*; import java.awt.event.*; import javax.swing.*; class Example extends JFrame { // Declaration of objects of JLabel class. JLabel l1, l2, l3, l4, l5; // Constructor of Example class. public Example() { // Creating Object of "FlowLayout" class FlowLayout layout = new FlowLayout(); // this Keyword refers to current object. // Function to set Layout of JFrame. this.setLayout(layout); // Initialization of object "l1" of JLabel class. l1 = new JLabel("Label 1 "); // Initialization of object "l2" of JLabel class. l2 = new JLabel("Label 2 "); // Initialization of object "l3" of JLabel class. l3 = new JLabel("Label 3 "); // Initialization of object "l4" of JLabel class. l4 = new JLabel("Label 4 "); // Initialization of object "l5" of JLabel class. l5 = new JLabel("Label 5 "); // this Keyword refers to current object. // Adding Jlabel "l1" on JFrame. this.add(l1); // Adding Jlabel "l2" on JFrame. this.add(l2); // Adding Jlabel "l3" on JFrame. this.add(l3); // Adding Jlabel "l4" on JFrame. this.add(l4); // Adding Jlabel "l5" on JFrame. this.add(l5); } } class MainFrame { // Driver code public static void main(String[] args) { // Creating Object of Example class. Example f = new Example(); // Function to set title of JFrame. f.setTitle("Example of FlowLayout"); // Function to set Bounds of JFrame. f.setBounds(200, 100, 600, 400); // Function to set visible status of JFrame. f.setVisible(true); } } Output: We can control the alignment of components in a flow layout arrangement, by using these FlowLayout Fields. 1) RIGHT :- Each row of component shifts towards right. 2) LEFT :- Each row of component shifts towards left. Program 2: The following program illustrates the use of FlowLayout using Right alignment by passing the argument FlowLayout.RIGHT in the constructor of FLowLayout. We create 5 JLabel components named "l1", "l2"... "l5" and then add them to the JFrame by the method this.add(). We set the title and bounds of the frame by method setTitle and setBounds. The layout is set by the method setLayout(); Java // Java program to show example of // FlowLayout and using RIGHT alignment import java.awt.*; import java.awt.event.*; import javax.swing.*; class Example extends JFrame { // Declaration of objects of JLabel class. JLabel l1, l2, l3, l4, l5; // Constructor of Example class. public Example() { // Creating Object of "FlowLayout" class, passing // RIGHT alignment through constructor. FlowLayout layout = new FlowLayout(FlowLayout.RIGHT); // this Keyword refers to current object. // Function to set Layout of JFrame. this.setLayout(layout); // Initialization of object "l1" of JLabel class. l1 = new JLabel("Label 1 "); // Initialization of object "l2" of JLabel class. l2 = new JLabel("Label 2 "); // Initialization of object "l3" of JLabel class. l3 = new JLabel("Label 3 "); // Initialization of object "l4" of JLabel class. l4 = new JLabel("Label 4 "); // Initialization of object "l5" of JLabel class. l5 = new JLabel("Label 5 "); // this Keyword refers to current object. // Adding Jlabel "l1" on JFrame. this.add(l1); // Adding Jlabel "l2" on JFrame. this.add(l2); // Adding Jlabel "l3" on JFrame. this.add(l3); // Adding Jlabel "l4" on JFrame. this.add(l4); // Adding Jlabel "l5" on JFrame. this.add(l5); } } class MainFrame { // Driver code public static void main(String[] args) { // Creating Object of Example class. Example f = new Example(); // Function to set title of JFrame. f.setTitle("Example of FlowLayout"); // Function to set Bounds of JFrame. f.setBounds(200, 100, 600, 400); // Function to set visible status of JFrame. f.setVisible(true); } } Output: Reference: https://www.geeksforgeeks.org/java/message-dialogs-java-gui/ Create Quiz Comment A Amaninder.Singh Follow 1 Improve A Amaninder.Singh Follow 1 Improve Article Tags : Misc Java Java-AWT Explore Java BasicsIntroduction to Java3 min readJava Programming Basics9 min readJava Methods6 min readAccess Modifiers in Java4 min readArrays in Java7 min readJava Strings7 min readRegular Expressions in Java3 min readOOP & InterfacesClasses and Objects in Java5 min readAccess Modifiers in Java4 min readJava Constructors4 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages2 min readJava Interface7 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java4 min readIterator in Java4 min readJava Comparator Interface5 min readException HandlingJava Exception Handling6 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java7 min readFile Handling in Java4 min readJava Method References9 min readJava 8 Stream Tutorial7 min readJava Networking6 min readJDBC Tutorial5 min readJava Memory Management3 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples7 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions5 min readJava Quiz1 min readJava Project Ideas For Beginners and Advanced15+ min read Like