0% found this document useful (0 votes)
12 views9 pages

UNIT5 LayoutManagers Part1

Java Swing is a part of the Java Foundation Classes (JFC) used for creating platform-independent and lightweight window-based applications, built on the AWT API. It offers a range of components and supports a pluggable look and feel, following the MVC architecture for clear separation of logic. Layout managers like BorderLayout, GridLayout, FlowLayout, and CardLayout are utilized to arrange components in various formats within a GUI.

Uploaded by

nlr.reddy135
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views9 pages

UNIT5 LayoutManagers Part1

Java Swing is a part of the Java Foundation Classes (JFC) used for creating platform-independent and lightweight window-based applications, built on the AWT API. It offers a range of components and supports a pluggable look and feel, following the MVC architecture for clear separation of logic. Layout managers like BorderLayout, GridLayout, FlowLayout, and CardLayout are utilized to arrange components in various formats within a GUI.

Uploaded by

nlr.reddy135
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

Introduction to Java Swing

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.

Unlike AWT, Java Swing provides platform-independent and lightweight components.

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.

Difference Between AWT and Swing


Java AWT Java Swing

AWT components are platform-dependent. Java swing components are platform-independent.

AWT components are heavyweight. Swing components are lightweight.

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.

AWT doesn't follows MVC(Model View Controller) Swing follows MVC.


where model represents data, view represents
presentation and controller acts as an interface
between model and view.
MVC Architecture in Java
Features of MVC :

• It provides a clear separation of business logic, Ul logic, and input logic.


• It offers full control over your HTML and URLs which makes it easy to
design web application architecture.
• It is a powerful URL-mapping component using which we can build
applications that have comprehensible and searchable URLs.
• It supports Test Driven Development (TDD).

Components of MVC:
The MVC framework includes the following 3 components:
• Controller
• Model
• View

Fig : Architecture of MVC


Controller:

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.

Understanding Layout Managers


The LayoutManagers are used to arrange components in a particular manner. The Java
LayoutManagers facilitates us to control the positioning and size of the components
in GUI forms. LayoutManager is an interface that is implemented by all the classes of
layout managers. There are the following classes that represent the layout managers:

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:

1. public static final int NORTH


2. public static final int SOUTH
3. public static final int EAST
4. public static final int WEST
5. public static final int CENTER

Constructors

o BorderLayout(): creates a border layout but with no gaps between the


components.
o BorderLayout(int hgap, int vgap): creates a border layout with the given
horizontal and vertical gaps between the components.

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.

Constructors of GridLayout class


1. GridLayout(): creates a grid layout with one column per component in a row.
2. GridLayout(int rows, int columns): creates a grid layout with the given rows and
columns but no gaps between the components.

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.

Constructors of FlowLayout class


FlowLayout(): creates a flow layout with centered alignment and a default 5 unit horizontal
and vertical gap.
FlowLayout(int align): creates a flow layout with the given alignment and a default 5 unit
horizontal and vertical gap.

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.

Constructors of CardLayout Class


CardLayout(): creates a card layout with zero horizontal and vertical gap.
CardLayout(int hgap, int vgap): creates a card layout with the given horizontal and
vertical gap.
Commonly Used Methods of CardLayout Class

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:

When the button named apple is clicked, we get another button.

You might also like