Do you think IIT Guwahati certified course can help you in your career?
No
Introduction
One of AWT's layout managers, FlowLayout, is used in applets to organize the components, from left to right. When there are more components than there is an increase in the size window, Java by default allows FlowLayout to organize the components so that they fit in the windowpane. The default options for FlowLayout are center alignment and five-pixel horizontal and vertical spacing between components.
In this article, we will discuss flowlayout in java, the types of grid layout constructors, swing class in java, fields, functions of flowlayout, and examples of grid layout. We will start with the brief introduction to flowlayout in java and swing class.
What is Java Swing?
The Java Swing toolkit, which is used to create graphical user interfaces (GUIs) in Java, contains a considerable number of widgets. It is the part of a Java Foundation Classes and comes with several packages for creating Java-rich desktop applications (JFC). Swing has a variety of built-in capabilities for displaying HTTP or rich text format, including trees, picture buttons, tabbed windows, sliders, toolbars, color pickers, tables, and text areas (RTF). Swing components are platform-independent because they were entirely developed in Java.
Class flowlayout extends Object
Implements LayoutManager, Serializable
Fields
Field name
Functions
Public static int CENTER
It centers each component row.
Public static int LEADING
This value tells the container's orientation that each row of components should be justified to the leading edge. For instance, in left-to-right orientations, to the left.
Public static int LEFT
Indicates that each component row should be left-justified.
Public static int RIGHT
Indicates that each component row should be justified to the right.
Public static int TRAILING
Each row of components should be justified to the trailing edge of the container's orientation. For instance, in left-to-right orientations, to the right.
Functions of Flowlayout in Java
1. setAlignment(int align): Adjusts the alignment in accordance with the container's layout.
2. getAlignment(): Returns the container's layout's alignment.
3. setTitle(String text): Sets the container's title using the provided text.
4. addLayoutComponent(): It adds the specific component to the layout.
Constructor in flowlayout in Java
Here is the extracted text from the image:
CONSTRUCTORS
FUNCTIONS
flowlayout()
Creates a center-aligned instance of FlowLayout with a 5-pixel space between each component.
flowlayout(int align)
Creates a flow layout with a 5-pixel gap between each component using the specified alignment.
flowlayout(int align, int horizontalgap, int verticalgap)
Creates a flow layout with a given alignment and a given horizontal and vertical gap between the components.
Now let’s understand the types of flowlayout constructor with the help of some examples
1. Java flowlayout() constructor
Java
Java
//program to implement the flowlayout in java with constructor. import java.awt.*; import javax.swing.*;
public class Flowlayout { // making a object of the frame. JFrame frameObject;
Flowlayout() { // frame object frameObject = new JFrame();
// creating the buttons to display. JButton button1 = new JButton("C"); JButton button2 = new JButton("O"); JButton button3 = new JButton("D"); JButton button4 = new JButton("I"); JButton button5 = new JButton("N"); JButton button6 = new JButton("G"); JButton button7 = new JButton("N"); JButton button8 = new JButton("I"); JButton button9 = new JButton("N"); JButton button10 = new JButton("J"); JButton button11 = new JButton("A"); JButton button12 = new JButton("S");
// add the buttons to frame frameObject.add(button1); frameObject.add(button2); frameObject.add(button3); frameObject.add(button4); frameObject.add(button5); frameObject.add(button6); frameObject.add(button7); frameObject.add(button8); frameObject.add(button9); frameObject.add(button10); frameObject.add(button11); frameObject.add(button12);
frameObject.setLayout(new FlowLayout()); // changing width and height of the frame. frameObject.setSize(300, 300); //making the visibility to true. frameObject.setVisible(true); }
// calling the main method public static void main(String argvs[]) { //calling the Flowlayout constructor. new Flowlayout(); } }
You can also try this code with Online Java Compiler
// add buttons to the frame frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.add(button7); frame.add(button8); frame.add(button9); frame.add(button10); // set up the flow layout of right alignment frame.setLayout(new FlowLayout(FlowLayout.LEFT)); //setting up the width and height of the frame. frame.setSize(300,300); //setting up the visibility to true. frame.setVisible(true); } public static void main(String[] args) { //calling the constructor. new flowlayoutexample(); } }
You can also try this code with Online Java Compiler
// add buttons to the frame frame.add(button1); frame.add(button2); frame.add(button3); frame.add(button4); frame.add(button5); frame.add(button6); frame.add(button7); frame.add(button8); // making the allignment to right // horizontal gap is 20 units and vertical gap is 25 units. frame.setLayout(new FlowLayout(FlowLayout.RIGHT, 20, 25));
//set up the width and height of the frame. frame.setSize(300, 300); frame.setVisible(true); } // main method public static void main(String argvs[]) { //calling the constructor. new flowlayoutexample(); } }
You can also try this code with Online Java Compiler
When there is not enough space, a FlowLayout for Android allows child views to flow to the next row. In order to evenly distribute the views, the FlowLayout can determine the distance between child views.
Describe the working of BorderLayout?
A border arrangement places a container's contents in five different locations: the north, south, east, west, and center. There can only be one component in each region, which is denoted by the constants NORTH, SOUTH, EAST, WEST, and CENTER.
What is FlowLayout and GridLayout in Java?
FlowLayout arranges components in a left-to-right flow, wrapping to the next line when needed, similar to text. GridLayout organizes components in a grid of equal-sized cells, arranging them in rows and columns.
What is flow layout in Java?
FlowLayout is a layout manager in Java that arranges components in a left-to-right flow, moving to the next line when there’s no space left. It ensures components appear in their natural size.
Conclusion
In this article, we have thoroughly covered FlowLayout in Java, including the Java Swing class, how to declare the FlowLayout class, its fields, functions, and constructors. We have also explored various FlowLayout constructors with examples to help you understand its usage better.