UNIT5 LayoutManagers Part1
UNIT5 LayoutManagers Part1
Swing is a Java Foundation Classes [JFC] library and an extension of the Abstract
Window Toolkit [AWT].
Java Swing is a part of Java Foundation Classes (JFC) that is used to create window-
based applications. It is built on the top of AWT (Abstract Windowing Toolkit) API and
entirely written in java.
The javax.swing package provides classes for java swing API such as JButton, JTextField,
JTextArea, JRadioButton, JCheckbox, JMenu
Features of Swings.
• Swing is a Set Of API (API- Set Of Classes and Interfaces)
• Swing is Provided to Design Graphical User Interfaces
• Swing is an Extension library to the AWT (Abstract Window Toolkit)
• Swing is Entirely written in Java
• Java Swing Components are Platform-independent And The Swing
Components are lightweight.
• Swing Supports a Pluggable look and feels And Swing provides more
powerful components
• Further Swing Follows MVC.
AWT doesn't support pluggable look and feel. Swing supports pluggable look and feel.
AWT provides less components than Swing. Swing provides more powerful components such
as tables, lists, scrollpanes, colorchooser, tabbedpane
etc.
Components of MVC:
The MVC framework includes the following 3 components:
• Controller
• Model
• View
The controller is the component that enables the interconnection between the
views and the model so it acts as an intermediary. It just tells the model what to do.
It processes all the business logic and incoming requests, manipulates data using
the Model component, and interact with the View to render the final output.
View:
The View component is used for all the UI logic of the application. It generates a
user interface for the user. Views are created by the data which is collected by the
model component but these data aren’t taken directly but through the controller. It
only interacts with the controller.
Model:
The Model component corresponds to all the data-related logic that the user works
with. This can represent either the data that is being transferred between the View
and Controller components or any other business logic-related data. It can add or
retrieve data from the database. It responds to the controller’s request because the
controller can’t interact with the database by itself. The model interacts with the
database and gives the required data back to the controller.
1. BorderLayout
2. FlowLayout
3. GridLayout
4. CardLayout
1.BorderLayout
The BorderLayout is used to arrange the components in five regions: north, south,
east, west, and center. Each region (area) may contain one component only. It is the
default layout of a frame or window. The BorderLayout provides five constants for
each region:
Constructors
import java.awt.*;
import javax.swing.*;
public class Border
{
JFrame f;
Border()
{
f = new JFrame();
JButton b1 = new JButton("NORTH");
JButton b2 = new JButton("SOUTH");
JButton b3 = new JButton("EAST");
JButton b4 = new JButton("WEST");
JButton b5 = new JButton("CENTER");
f.add(b1, BorderLayout.NORTH);
f.add(b2, BorderLayout.SOUTH);
f.add(b3, BorderLayout.EAST);
f.add(b4, BorderLayout.WEST);
f.add(b5, BorderLayout.CENTER);
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String[] args) {
new Border();
}
}
Output Window:
2. GridLayout
The Java GridLayout class is used to arrange the components in a rectangular grid. One
component is displayed in each rectangle.
import java.awt.*;
import javax.swing.*;
public class MyGridLayout{
JFrame f;
MyGridLayout(){
f=new JFrame();
JButton b1=new JButton("1");
JButton b2=new JButton("2");
JButton b3=new JButton("3");
JButton b4=new JButton("4");
JButton b5=new JButton("5");
JButton b6=new JButton("6");
JButton b7=new JButton("7");
JButton b8=new JButton("8");
JButton b9=new JButton("9");
f.add(b1); f.add(b2); f.add(b3);
f.add(b4); f.add(b5); f.add(b6);
f.add(b7); f.add(b8); f.add(b9);
f.setLayout(new GridLayout(3,3));
f.setSize(300,300);
f.setVisible(true);
}
public static void main(String[] args) {
new MyGridLayout();
}
}
3.FlowLayout
The Java FlowLayout class is used to arrange the components in a line, one after another (in
a flow). It is the default layout of the applet or panel.
import java.awt.*;
import javax.swing.*;
public class FlowLayoutExample
{
JFrame f;
FlowLayoutExample()
{
f = new JFrame();
JButton btn1 = new JButton("1");
JButton btn2 = new JButton("2");
JButton btn3 = new JButton("3");
JButton btn4 = new JButton("4");
JButton btn5 = new JButton("5");
JButton btn6 = new JButton("6");
f.add(btn1); f.add(btn2); f.add(btn3);
f.add(btn4); f.add(btn5); f.add(btn6);
f.setLayout(new FlowLayout());
f.setSize(300, 300);
f.setVisible(true);
}
public static void main(String argvs[])
{
new FlowLayoutExample();
}
}
Output:
4.CardLayout
The Java CardLayout class manages the components in such a manner that only one
component is visible at a time. It treats each component as a card that is why it is
known as CardLayout.
public void next(Container parent): is used to flip to the next card of the given
container.
public void previous(Container parent): is used to flip to the previous card of the
given container.
public void first(Container parent): is used to flip to the first card of the given
container.
public void last(Container parent): is used to flip to the last card of the given
container.
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
public class CardLayoutExample extends JFrame implements ActionListener
{
CardLayout crd;
JButton btn1, btn2, btn3;
Container cPane;
CardLayoutExample()
{
cPane = getContentPane();
crd = new CardLayout();
cPane.setLayout(crd);
btn1 = new JButton("Apple");
btn2 = new JButton("Orange");
btn3 = new JButton("Banana");
btn1.addActionListener(this);
btn2.addActionListener(this);
btn3.addActionListener(this);
cPane.add("a", btn1);
cPane.add("b", btn2);
cPane.add("c", btn3);
}
public void actionPerformed(ActionEvent e)
{
crd.next(cPane);
}
public static void main(String argvs[])
{
CardLayoutExample crdl = new CardLayoutExample();
crdl.setSize(300, 300);
crdl.setVisible(true);
}
}
Output: