FlowLayout
FlowLayout as Layout Manager in Java
Details: FlowLayout
FlowLayouts are a design pattern used in Android development. It is a flexible layout that allows users to arrange elements within a container in a linear manner. It’s mainly used for text and images, but can be applied to other elements as well. Developers can use a FlowLayout to create engaging user interfaces.
This post will teach us how to create a program with a FlowLayout Manager in Java. FlowLayout is the basic format used as default by the JPanel. In other to illustrate this, the following steps can be taken;
1 – Create a java program with a file name of flowLayout.java in JCreator or NetBeans.
2 – Proceed to import the packages below:
import javax.swing.*; // used to access the JFrame, JRadioButton, ButtonGroup, and Container class
import java.awt.*; // used to access the FlowLayout class
import java.awt.event.*; //used to access the ActionEvent and ActionListener class
3 – Extend to JFrame and implement it with an ActionListener in your class because the event will be used in the radio buttons. Make use of the listed variable below;
public class flowLayout extends JFrame implements ActionListener {
JRadioButton rbLeft = new JRadioButton("Left");
JRadioButton rbCenter = new JRadioButton("Center");
JRadioButton rbRight = new JRadioButton("Right");
ButtonGroup bg = new ButtonGroup();
Container con = getContentPane();
FlowLayout layout = new FlowLayout();
It can be highlighted that there are 3 radio buttons with names, Left, Right, and Center catch. Also instated is a variable bg as ButtonGroup to be used to group the radio button, another variable con as a Container that provides the pane as a container of the components.
There is a variable layout that helps in using the stream format declared a FlowLayout class.
4 – Proceed by manking a constructor and name it flowLayout.
We can now add the radio button components by using the add method of our container variable.
con.add(rbLeft);
con.add(rbCenter);
con.add(rbRight);
In other to group the radio buttons, we can utilize the add method of the ButtonGroup variable.
bg.add(rbLeft);
bg.add(rbRight);
bg.add(rbCenter);
The addActionListener method will aid us in adding the action listener of the radio buttons and the parameter “this” inside the radio button. The keyword “this” signifies that it is in the current file.
rbLeft.addActionListener(this);
rbRight.addActionListener(this);
rbCenter.addActionListener(this);
Proceed to use the setLayout method of the container and insert the variable layout as a FlowLayout.
con.setLayout(layout);
setDefaultCloseOperation(EXIT_ON_CLOSE);
5 – Using the code below, we will create a listener and the event for the radio buttons.
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == rbLeft)
{
layout.setAlignment(FlowLayout.LEFT);
}
else if (source == rbRight){
layout.setAlignment(FlowLayout.RIGHT);
}
else{
layout.setAlignment(FlowLayout.CENTER);
}
layout.layoutContainer(con);
}
In each radio button, there is an alignment for the FlowLayout utilizing the setAlignment strategy. The layoutContainer method implies that the layout utilized in the container is the FlowLayout.
6 – Finally, instantiate the class name or the constructor inside and name it as “frame” as a variable in your main. Then set the visibility, size, and title of the frame.
public static void main(String[] args) {
flowLayout frame = new flowLayout();
frame.setSize(300, 100);
frame.setVisible(true);
frame.setTitle("FlowLayout as Layout Manager");
}
Output: FlowLayout
Complete Source Code: FlowLayout
import javax.swing.*; // used to access the JFrame, JRadioButton, ButtonGroup, and Container class
import java.awt.*; // used to access the FlowLayout class
import java.awt.event.*; //used to access the ActionEvent and ActionListener class
public class flowLayout extends JFrame implements ActionListener {
JRadioButton rbLeft = new JRadioButton("Left");
JRadioButton rbCenter = new JRadioButton("Center");
JRadioButton rbRight = new JRadioButton("Right Button");
ButtonGroup bg = new ButtonGroup();
Container con = getContentPane();
FlowLayout layout = new FlowLayout();
public flowLayout() {
con.add(rbLeft);
con.add(rbCenter);
con.add(rbRight);
bg.add(rbLeft);
bg.add(rbRight);
bg.add(rbCenter);
rbLeft.addActionListener(this);
rbRight.addActionListener(this);
rbCenter.addActionListener(this);
con.setLayout(layout);
setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
Object source = e.getSource();
if(source == rbLeft)
{
layout.setAlignment(FlowLayout.LEFT);
}
else if (source == rbRight){
layout.setAlignment(FlowLayout.RIGHT);
}
else{
layout.setAlignment(FlowLayout.CENTER);
}
layout.layoutContainer(con);
}
public static void main(String[] args) {
flowLayout frame = new flowLayout();
frame.setSize(300, 100);
frame.setVisible(true);
frame.setTitle("FlowLayout as Layout Manager");
}
}