Advertisement



< Prev
Next >



FlowLayout Manager




FlowLayout manager class 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. The FlowLayout manager also maintains a considerable space between each component positioned within 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 the 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 the 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 the 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 the FlowLayout manager class.


Advertisement




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(Figure 1) that contains the components such as two labels, two textfields and three buttons within our container, JFrame.


Figure 1


Program Analysis


In this code, we have created an object FlowLayout by calling its first constructor, hence, by default, the 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 -




< Prev
Next >
< Swing Framework
BorderLayout Manager >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement