< Prev
Next >



FlowLayout Manager




FlowLayout manager, positions the components in a container within a line from left to right, starting from top to bottom. When one line is filled with the components, the next new component is added to the next line below. FlowLayout manager also maintains a considerable space between each component positioned within in a line and also between the lines that contain components.




Constructors of FlowLayout


Constructor Description
public FlowLayout() Creates a default FlowLayout object, which positions the components in center and maintains a space of 5 pixels between components in a container.
public FlowLayout(int align) Creates a FlowLayout which aligns the line holding the components according to the value of align constant.
public FlowLayout(int how, int horizontalSpace, int verticalSpace) Creates a Flowlayout, which aligns the line holding the components according to the value of align constant and also lets us specify the horizontal and vertical space between the components.





The constant align in FlowLayout, may take one of these values -:


Value of align constant Description
FlowLayout.RIGHT Positions the line containing the components to the right.
FLowLayout.LEFT Positions the line containing the components to the left.
FLowLayout.CENTER Positions the line containing the components in the center.
FlowLayout.LEADING Positions the line containing the components at the leading edge.
FlowLayout.TRAILING Positions the line containing the components at the trailing edge.



We can position the components in a container by calling a method that exists in each container class -

Method Description
public void setLayout(LayoutManager object) Positions the element in a Container.

LayoutManager is an interface which is implemented by FlowLayout manager class.




An example display the use of FlowLayout Manager.



import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

public class FlowLayoutEx 
{
public static void main(String... ar)
{
SwingUtilities.invokeLater(new Runnable() {
public void run()
{
new A();
}
});

}//Closing the main method
}//Closing the class A


class A 
{

JLabel label1, label2, label3;
JTextField field1, field2;
JButton button1, button2, button3;
JFrame jf;


A()
{
jf = new JFrame("FlowLayout Example");
jf.setSize(400,150);

label1= new JLabel("Enter your name");
label2= new JLabel("Enter your city");


field1 = new JTextField(15);	//calling TextField(int widthOfTextField)
field2 = new JTextField(15); 

button1 = new JButton("Clear");
button2 = new JButton("Submit");
button3 = new JButton("Exit");

//Setting the positioning of the components in container.
jf.setLayout(new FlowLayout()); // calling the first constructor of FlowLayout class, which
				// positions each line of components in the center of JFrame

jf.add(label1);	  		 //Adding the first Jlabel component to JFrame container
jf.add(field1);	  		 //Adding the first JTextField component to JFrame container
jf.add(label2);	  		 //Adding the second JLabel component to JFrame container
jf.add(field2);   		 //Adding the second JTextField component to JFrame container
jf.add(button1);   		 //Adding the first JButton component to JFrame container
jf.add(button2);   		 //Adding the second JButton component to JFrame container
jf.add(button3);   		 //Adding the third JButton component to JFrame container

jf.setSize(300,200);
jf.setVisible(true); 
}

}
When you run the code, you are presented a window that contains the components such as two labels, two textfields and three buttons within our container , JFrame.

Figure 1

Note1 : In this code, we have created an object FlowLayout by using calling its basic first constructor, hence, by default, component will be positioned centered and a default space of 5 pixels will be maintained between each component positioned in a line.





Please share this article -

Facebook Google Pinterest Reddit Tumblr Twitter



< Prev
Next >
< Swing Framework
BorderLayout Manager >
Please subscribe our social media channels for notifications, we post a new article everyday.

Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

Coming Next
-
C++ and Python

Ad2