FlowLayout as Layout Manager in Java

May 27, 2020
Tutorials
flowlayout

This is an instructional exercise where we will going to make a program with a FlowLayout as Layout Manager in Java. FlowLayout, as a design administrator gives a format that is simple and utilized as default by the JPanel, it makes it each part observed by its preferred size and organizes them in horizontal wrapping lines with the goal that they will have spacing.

So, now let’s start this tutorial!

1. Open JCreator or NetBeans and make a java program with a file name of flowLayout.java.

2. Import the following packages:

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. In your class, extends it to JFrame and implement it with an ActionListener because we will use such event in the radio buttons. Initialize the following variables 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();

 

As you have seen the code above; we have three radio buttons named Left, Right, and Center catch. We likewise instate variable bg as ButtonGroup so we can group the radio button. We also make variable con as a Container that will provide the pane as a container of the components. Additionally, to utilize the stream format, we have a variable layout declared as a FlowLayout class.

4. Now, we will make a constructor named flowLayout.

Inside of this, we add the radio button components by using the add method of our container variable.

con.add(rbLeft);
con.add(rbCenter);
con.add(rbRight);

To group the radio buttons, we will use the add method of the ButtonGroup variable.

bg.add(rbLeft);
bg.add(rbRight);
bg.add(rbCenter);

We will add then the action listener of the radio buttons by using the addActionListener method and the parameter this inside the radio button. The this keyword indicates that it is in the current file.

rbLeft.addActionListener(this);
rbRight.addActionListener(this);
rbCenter.addActionListener(this);

Then use the setLayout method of the container and put the variable layout as a FlowLayout inside of it.

con.setLayout(layout);
setDefaultCloseOperation(EXIT_ON_CLOSE);

5. Now, we will create a listener and the event for the radio buttons. Have this code below:

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 every radio buttons, there is a corresponding alignment for the FlowLayout using the setAlignment method. The layoutContainer method means that the layout used in the container is the FlowLayout.

6. Now, in your main, instantiate the class name or the constructor inside and make it named as frame as a variable. Lastly set the size, visibility, 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:

output

flowlayout

output


Here’s the full code of this tutorial:

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");
}
}

 

Facebook Comments

Leave a Reply

Your email address will not be published.