Introduction to FlowLayout in Java
- FlowLayout is one of AWT’s layout managers used in applets to arrange the components in a manner from left to right, just like words in a paragraph.
- When no. Of components increases than the window size, then by default, Java enables FlowLayout to arrange the components to fit in the windowpane.
- FlowLayout is the default layout provided by the layout manager. When you do not select any layout, then the layout will be set to flow.
- FlowLayout uses some default settings such as center alignment with five pixels gaps between components horizontally and vertically.
The “align” property determines the alignment. And alignment field is as follows:
- LEFT: It aligns components to the left.
- RIGHT: It aligns components to the right.
- CENTER: It aligns components to the center.
- LEADING: It will be at the leading edge of the container, i.e. to the left for the left to right orientation.
- TRAILING: It will be at the trailing edge of the container, i.e. to the right for the left to right
Basic Concepts of Flowlayout in Java
Before deep-diving into FlowLayout in Java details, let us revise some basic topics required for FlowLayout :
Java AWT: AWT is an API for building GUI or window-based applications.
Some of the basic concepts regarding AWT hierarchy are as follows:
1. Container: Container is a class that contains all the components such as button, textfield, titlebar, menubar, etc.
2. Components: Components are the GUI objects such as buttons, label, textfield, checklist.
There are some basic components functions such as:
- public void adds (Component c): adds a component on this component.
- public void setSize(int width, int height): sets the component’s width and height.
- public void setLayout(LayoutManager m): defines the layout manager for the component.
- public void setVisible(boolean status): defines the visibility of the component; by default, it is false.
3. Panel: Panel is a container class. It creates a space for an application where all the components can be fit in. It inherits the container class.
The panel does not contain a menubar or titlebar but can contain other components like textfield and buttons.
4. Frame: Frame is a container class. It contains a menu bar and title bar and can contain other components as well.
5. Layout Manager: A layout manager is an object that implements the LayoutManager interface to determine the size and position of the components within the container.
There are many types of layout manager available, each with its own capabilities and specific tasks:
- FlowLayout.
- BorderLayout.
- CardLayout.
- BoxLayout.
- GridLayout.
- GridBagLayout.
- GroupLayout.
- SpringLayout.
Commonly used functions of FlowLayout
- setAlignment(int align): Sets the respective alignment to the layout of the container.
- getAlignment(): Gets the alignment of the layout of the container.
- addLayoutComponent(): Adds the particular component to the layout.
- setTitle(String text): Sets the title of the container with the given text.
Constructors in FlowLayout
- FlowLayout(): Constructs an instance of FlowLayout as center-aligned and with 5 pixels gap between the components.
- FlowLayout(int align): Constructs a FlowLayout with a given alignment with 5 pixels gap between the components.
- FlowLayout(int align, int horizontalGap, int verticalGap): Constructs a FlowLayout with a given alignment and with a given horizontal and vertical gap between the components.
- This constructor will align by the specified align field as RIGHT, LEFT or CENTER and also provides the option for adding horizontal gap and the vertical gap between components.
Example of Flowlayout in Java
Following is an example of flow layout in java:
Code:
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Layout extends JFrame
{
private JButton lb;
private JButton rb;
private JButton cb;
private FlowLayout layout;
private Container container;
public Layout()
{
super("the title");
layout = new FlowLayout();
container = getContentPane();
setLayout(layout); //left alignment
lb = new JButton("left");
add(lb);
lb.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
layout.setAlignment(FlowLayout.LEFT);
layout.layoutContainer(container);
}
}
);
//right alignment
rb = new JButton("right");
add(rb);
rb.addActionListener(
new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
layout.setAlignment(FlowLayout.RIGHT);
layout.layoutContainer(container);
}
}
);
//center alignment
cb = new JButton("center");
add(cb);
cb.addActionListener(
new ActionListener(){
public void actionPerformed(ActionEvent event)
{
layout.setAlignment(FlowLayout.CENTER);
layout.layoutContainer(container);
}
}
);
}
}
Main Method of Flowlayout in Java:
import javax.swing.*;
public class Test
{
public static void main(String[] args)
{
Layout l = new Layout();
l.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
l.setSize(300, 100);
l.setVisible(true);
}
}
Output:
The explanation for the above code:
- In the above code, we had declared 3 buttons with the help of JButton as lb, rb, and cb.
- When creating the class instance, we need to see the buttons so adding the buttons to a container in the class constructor itself.
- We extend the JFrame class
- With the setLayout() functions, we are setting the layout as flowlayout.
- When we click on a button named “left” for left alignment, the buttons on the window-pane should be aligned to the left.
- For this to work, the particular button is added on the window panel and on the click event of that button, the layout will be set to Left.
- In the above code, setAlignment(FlowLayout.LEFT) will set the panel layout alignment to left. FlowLayout.LEFT is the field available for alignment. Similarly, FlowLayout.RIGHT, FlowLayout.CENTER, FlowLayout.TRAILING, FlowLayout.LEADING are some of the other fields used to set alignment.
- Please note that these alignment fields are going to align all the components to either left, right or center.
- It does not depend on the size of your window. It will adjust and align your components with the specified alignment respectfully.
- As we similarly did for left alignment, we developed for the center and right alignment. Finally, we developed Test Class for writing the main method to test our FlowLayout Java code.
- By creating an instance of the Layout class in the main method, we set the window size and component visibility to true.
Conclusion
This tutorial gives you the basic idea of why Java has FlowLayout and its functionalities. FlowLayout is the default layout because of its ease and alignment precision. Remember, every concept gets even more clear when you code and practice it. Let error come in your code, do not hesitate to keep coding and understand the concept precisely.
Recommended Articles
This is a guide to Flowlayout in Java. Here we discuss the basic concept, constructors and Commonly used functions of FlowLayout. You may also look at the following article to learn more –