Layout Managers
Layout Managers
TECHNOLOGY
• FlowLayout
• BorderLayout
• GridLayout
• GridBagLayout
• CardLayout
• GroupLayout
• BoxLayout
• NullLayout
Java--Layout
Managers
Layout Managers
• A layout Manager arranges the child components of
a Container
• FlowLayout
• GridLayout
• BorderLayout
• CardLayout
• GridBagLayout
• To set a particular layout manager, an object of that type is created first and then
the setLayout method is used
FlowLayout
• FlowLayout is a simple layout manager that tries to arrange components with
their preferred sizes,from left to right and top to bottom in the display.
• Components are added to the container from left to right. When there is no
space in a row, components get added in the next row from left to right and so
on.
import java.awt.*;
import java.awt.event.*;
class frame1 extends Frame
{
frame1(String s)
{
super(s);
setLayout(new FlowLayout());
for(int i=1;i<=9;i++)
add(new Button("Button no:" +i));
setVisible(true);
setSize(300,300);
}
public static void main(String args[])
{
frame1 f=new frame1("FlowLayout");}}
GridLayout
• Components are arranged in the form of Grid consisting of rows and
columns.
• Components are added from left to right and top to bottom in the grid
• Components are arbitrarily resized to fit in the resulting areas; their
minimum and preferred sizes are consequently ignored.
• GridLayout is most useful for arranging very regularly ,identically sized
objects
• GridLayout takes the number of rows and columns in a constructor
GridLayout (3,2)
Grid Layout can be created :
import java.awt.*;
import java.applet.*;
/*<applet code=border1 width=300 height=400>
</applet>*/
public class border1 extends Applet
{
public void init()
{
Button b1;
Button b2;
Button b3;
Button b4;
BorderLayout b=new BorderLayout();
setLayout(b);
b1=new Button("hello");
b2=new Button("hi");
b3=new Button("wow");
b4=new Button("anu");
add("North" ,b1);
add("South",b2);
add("East",b3);
add("West",b4);}}
BorderLayout Program
import java.awt.*;
import java.awt.event.*;
class border1 extends Frame
{
border1(String s)
{
super(s);
setLayout(new BorderLayout());
add(new Button("North"),BorderLayout.NORTH);
add(new Button("South"),BorderLayout.SOUTH);
add(new Button("East"),BorderLayout.EAST);
add(new Button("West"),BorderLayout.WEST);
add(new Button("Center"),BorderLayout.CENTER);
setVisible(true);
setSize(300,300);
}
public static void main(String args[])
{
border1 f=new border1("FlowLayout");}}
USE of Three Layout Managers
import java.awt.*;
import java.applet.*;
/*<applet code=flow1 width=300 height=400>
</applet>*/
public class flow1 extends Applet
{
Panel p1=new Panel();
Panel p2=new Panel();
GridLayout gl=new GridLayout(2,2,2,2);
BorderLayout b=new BorderLayout (2,2);
Button b1=new Button("l1");
Button b2=new Button("l2");
Button b3=new Button("l3");
Button b4=new Button("l4");
Button b5=new Button("l5");
Button b6=new Button("l6");
Button b7=new Button("l7");
Button b8=new Button("l8");
public void init()
{
p1.setLayout(gl);
p2.setLayout(b);
//Add buttons to panels
p1.add(b1);
p1.add(b2);
p1.add(b3);
p1.add(b4);
p1.add(b5);
p1.setBackground(Color.red);
//Add panel p1 to p2
p2.add("North",p1);
p2.add("West",b5);
p2.add("East",b6);
p2.add("South",b7);
p2.add("Center",b8);
p1.setBackground(Color.green);
add(p2);}}
GridBag Layout
• GridBagLayout is a very flexible layout manager that allows
you to position the component relative to one another using
constraints
• With GridBagLayout you can create almost any imaginable
layout
• A row or column in a GrodBagLayout expands to
accommodate the dimensions and constraints of the largest
component in its ranks
• Individual components may span more than one row and
column
GridBag Layout
A grid bag layout differs from grid layout manager as mentioned below:
import java.awt.*;
import java.applet.*;
/*<applet code=gridbag1 width=300 height=400>
</applet>*/
public class gridbag1 extends Applet
{
GridBagConstraints gbc=new GridBagConstraints();
• setText(String)
• getText()
JRadioButton class
• JRadioButton()
• JRadioButton(String s)
• JRadioButton( String s ,Boolean select)
• setText(String)//methods
• getText()
JTextArea class
• JTextArea(String s)
• JTextArea(String s,Boolean select)
JLabel
import java.awt.*;
import javax.swing.*;
t=new JTextField(15);
contentpane.add(t);
}
public void actionPerformed(ActionEvent e)
{
t.setText(e.getActionCommand());
}
}
The JFrame Class
Frames are a powerful feature of Swing. You can create a window for your
application by using the JFrame class.
52
JPanel class Cont….
// Customer.java
Customer.java
53
The JRadioButton Class
JRadioButton r1,r2,r3,r4;
r1=new JRadioButton(“String”);
Customer1.java
54
The JRadioButton Class
Cont..
JRadioButton r1,r2;
ButtonGroup g1;
r1=new JRadioButton(“String”);
g1= new ButtonGroup();
g1.add(r1);
g1.add(r2);
Customer2.java
55
The JPasswordField Class
JPasswordField tp1;
JTextArea ta1;
tp1=new JPasswordField(10);
ta1= new JTextArea(row, column);
ta1= new JTextArea(3,8);
Customer3.java
56
JFrame Window
Example of Jframe window:
Fr2.java
57
The JMenu, JMenuBar, JMenuItem
Class
JMenuBar first;
Jmenu one , two, three;
JMenuItem m1,m2,m3,m4,m5,m6,m7;
Fr3.java
58
The Scrollbar Class
It is specified by integer values that are set during Scrollbar
construction.
Scrollbar sb;
sb = new Scrollbar(Scrollbar.X, initial_value, slider_width, range_from,
range_to);
Scrl.java
59
Example of Swing using FlowLayout
import javax.swing.*;
import java.awt.*;
/*<applet code=grid1 width=300 height=400>
</applet>*/
public class grid1 extends JApplet {
public void init() {
//create a variable that references
//the content pane of the applet
Container appletContainer = getContentPane();
// set the layout to FlowLayout
appletContainer.setLayout( new FlowLayout() );
// creates five buttons
JButton button1 = new JButton("Button1");
JButton button2 = new JButton("Button2");
JButton button3 = new JButton("Button3");
// add the buttons to the applet's content pane
appletContainer.add(button1);
appletContainer.add(button2);
appletContainer.add(button3);
}
}
import javax.swing.*;
import java.awt.*;
public class grid1 {
public static void main(String arg[]) {
JFrame frame = new JFrame("SetBounds Method Test");
frame.setSize(375, 250);
// Setting layout as null
frame.setLayout(null);
// Creating Button
JButton button = new JButton("Hello Java");
// Setting position and size of a button
button.setBounds(80,30,120,40);
frame.add(button);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Java AWT TextField Example with ActionListener
import java.awt.event.*;
public class grid1 extends Frame implements ActionListener{
TextField tf1,tf2,tf3;
Button b1,b2;
grid1(){
tf1=new TextField();
tf1.setBounds(50,50,150,20);
tf2=new TextField();
tf2.setBounds(50,100,150,20);
tf3=new TextField();
tf3.setBounds(50,150,150,20);
tf3.setEditable(false);
b1=new Button("+");
b1.setBounds(50,200,50,50);
b2=new Button("-");
b2.setBounds(120,200,50,50);
b1.addActionListener(this);
b2.addActionListener(this);
add(tf1);add(tf2);add(tf3);add(b1);add(b2);
setSize(300,300);
setLayout(null);
setVisible(true);
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText();
String s2=tf2.getText();
int a=Integer.parseInt(s1);
int b=Integer.parseInt(s2);
int c=0;
if(e.getSource()==b1){
c=a+b;
}else if(e.getSource()==b2){
c=a-b;
}
String result=String.valueOf(c);
tf3.setText(result);
}
public static void main(String[] args) {
new grid1();
} }
Box Layout
Box Layout
BoxLayout is used to arrange the component either vertically or horizontally
BoxLayout(Container c,int axis);// creates a box layout that arranges the components with the given axis.
import javax.swing.*;
c=getContentPane();
card=new CardLayout(40,30);
//create CardLayout object with 40 hor space and 30
ver space
c.setLayout(card);
b1=new JButton("Apple");
b2=new JButton("Boy");
b3=new JButton("Cat");
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.add("a",b1);c.add("b",b2);c.add("c",b3);
}
public void actionPerformed(ActionEvent e) {
card.next(c);
}
import javax.swing.*;
public class GroupExample {
public static void main(String[] args) {
JFrame frame = new JFrame("GroupLayoutExample");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
Container contentPanel = frame.getContentPane();
GroupLayout groupLayout = new
GroupLayout(contentPanel);
contentPanel.setLayout(groupLayout);
groupLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
.addComponent(clickMe)
.addComponent(button));
frame.pack();
frame.setVisible(true);
}
}
Null Layout
To use fix position of components we use Null Layout
THANK YOU