0% found this document useful (0 votes)
19 views18 pages

UNIT-5 Java

The document provides an overview of GUI application development in Java, focusing on AWT (Abstract Window Toolkit) and Swing. It explains the architecture, components, event handling, and layout managers of AWT, as well as the advantages and disadvantages of both AWT and Swing. Additionally, it covers Java applets, their lifecycle, and the differences between AWT and Swing, highlighting the features and capabilities of each toolkit.

Uploaded by

dipti.ltwt.in
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)
19 views18 pages

UNIT-5 Java

The document provides an overview of GUI application development in Java, focusing on AWT (Abstract Window Toolkit) and Swing. It explains the architecture, components, event handling, and layout managers of AWT, as well as the advantages and disadvantages of both AWT and Swing. Additionally, it covers Java applets, their lifecycle, and the differences between AWT and Swing, highlighting the features and capabilities of each toolkit.

Uploaded by

dipti.ltwt.in
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

UNIT – 5

GUI Application Development

Introduction to AWT (Abstract Window Toolkit)

AWT (Abstract Window Toolkit) is one of the oldest GUI toolkits in Java, provided in the [Link]
package. It is used to create Graphical User Interface (GUI) applications such as windows, buttons,
text fields, etc.

• It acts as a bridge between Java application and the operating system GUI

• Provides classes for window creation, event handling, drawing graphics, and user
interaction

Why “Abstract” Toolkit?

• It is called Abstract because it does not implement GUI components completely in Java.

• Instead, it relies on the native system (OS) to render components.

• For example:
o A button created in Java will look like a Windows button on Windows

o And a Mac button on macOS

Package Structure

AWT includes several important packages:

• [Link] → Core GUI components


• [Link] → Event handling

• [Link] → Image processing

• [Link] → Color handling


Key Components of AWT

1. Containers

Containers are used to hold and organize components.

• Frame

• Panel
• Dialog

1
2. Controls (Components)

Used for user interaction:

• Button

• Label
• TextField

• Checkbox

• List

Layout Managers

Used to arrange components:

• FlowLayout

• BorderLayout

• GridLayout
How AWT Works (Architecture)

• AWT uses peer-based architecture

• Each Java component has a corresponding native component (peer)

Example:

• Java Button → Windows Button (via OS)

This is why AWT is:

• Platform-dependent
• Called Heavyweight

Features of AWT

1. Heavyweight Components

• Components depend on OS resources

• Each component has a peer (native object)

• More memory usage

2. Native System Resource Usage


• Uses OS-level libraries

2
• Faster rendering in some cases

3. Basic GUI Support

• Limited number of components

• No advanced UI features (compared to Swing)


Advantages of AWT

• Simple and easy to use

• Direct OS interaction → faster execution

• Good for small GUI applications

Disadvantages of AWT

• Platform-dependent UI

• Limited components

• Outdated compared to Swing/JavaFX


• Heavyweight → performance issues in complex apps

Example of AWT Program-

3
AWT Controls

AWT provides various controls for user interaction:

• Label – Displays text

• Button – Clickable button


• TextField – Single-line input

• TextArea – Multi-line input

• Checkbox – Multiple selections

• Choice – Dropdown list

• List – Multiple selectable items

What is a Java Applet?

A Java Applet is a small Java program designed to run inside a web browser or an applet viewer.
It is mainly used to create interactive web applications, animations, and small tools.

• Applets are embedded in HTML pages using <applet> or <object> tags

• They do not have a main() method


• Execution is controlled by the browser or applet viewer

Key Characteristics of Applets

• Runs inside a web browser environment

• Inherits from the Applet class ([Link])

• GUI is usually created using AWT or Swing

• Works in a restricted environment (sandbox) for security

Types of Applets
1. Local Applet

o Stored on local system

o Loaded using file path

2. Remote Applet

o Stored on a remote server


o Loaded via URL

4
Life Cycle of an Applet

The lifecycle is controlled by the browser and consists of the following methods:

1. init()

• Called only once when the applet is first loaded


• Used for initialization (UI setup, variable initialization)

2. start()

• Called after init()

• Called every time the applet becomes active

• Used to start/resume execution (e.g., animations)

3. stop()

• Called when the applet becomes inactive

• Used to pause execution


4. destroy()

• Called when the applet is removed from memory

• Used for cleanup (closing resources)

Applet Life Cycle Flow

init() → start() → stop() → start() → stop() → destroy()

Basic Example of Java Applet

import [Link];

import [Link];

public class HelloApplet extends Applet {

public void init() {

// Initialization code

public void start() {


// Start or resume execution

5
}

public void paint(Graphics g) {

[Link]("Hello Applet!", 50, 50);

}
public void stop() {

// Pause execution

public void destroy() {

// Cleanup code

HTML Code to Run Applet


<html>

<body>

<applet code="[Link]" width="300" height="200"></applet>

</body>

</html>

Advantages of Applets

• Platform-independent
• Can create interactive web content

• Easy to embed in web pages

Disadvantages of Applets

• Requires Java plugin (not supported now)

• Slow loading and performance issues

• Security restrictions limit functionality

• Not supported in modern browsers

6
Important Points for Exams

• No main() method in Applet

• Uses lifecycle methods instead

• Runs inside browser (not standalone)


What are Layout Managers?

Layout Managers are objects used to automatically arrange GUI components (buttons, labels, etc.)
inside a container like a Frame, Panel, or Applet.
Instead of manually setting positions (using setBounds()), layout managers:

• Adjust components automatically


• Make UI responsive

• Handle resizing efficiently

Why Use Layout Managers?

• Platform-independent UI

• Automatic alignment and spacing


• Better screen adaptability

• Reduces manual coding effort

Types of Layout Managers

7
Description:

• Default layout for Panel and Applet

• Arranges components left to right

• Moves to next line when space is full (like text flow)


Features:

• Simple and easy to use

• Supports alignment: LEFT, CENTER, RIGHT

• Has horizontal and vertical gaps

Example:

import [Link].*;

class FlowExample {

public static void main(String[] args) {


Frame f = new Frame();

[Link](new FlowLayout());

[Link](new Button("One"));

[Link](new Button("Two"));

[Link](new Button("Three"));

[Link](300, 300);

[Link](true);
}

8
BorderLayout

Description:

• Divides container into 5 regions:


o North

o South

o East

o West

o Center
Features:

• Default layout for Frame

• Center takes maximum space


• Only one component per region

Example:

import [Link].*;

class BorderExample {

public static void main(String[] args) {


Frame f = new Frame();

9
[Link](new BorderLayout());

[Link](new Button("North"), [Link]);

[Link](new Button("South"), [Link]);

[Link](new Button("East"), [Link]);


[Link](new Button("West"), [Link]);

[Link](new Button("Center"), [Link]);

[Link](300, 300);

[Link](true);

3. GridLayout

Description:

• Arranges components in rows and columns


• All components are of equal size

10
Features:

• No gaps unless specified


• Components fill entire container
• Best for forms and calculators

Example:

import [Link].*;
class GridExample {
public static void main(String[] args) {
Frame f = new Frame();
[Link](new GridLayout(2, 2));
[Link](new Button("1"));
[Link](new Button("2"));
[Link](new Button("3"));
[Link](new Button("4"));
[Link](300, 300);
[Link](true);
}
}

Working with Images

• Images can be loaded and displayed using Image class.

Methods:

• getImage() – Load image

• drawImage() – Display image

Graphics in Java
• Used for drawing shapes, text, and images.

Common Methods:

• drawLine()

• drawRect()

• drawOval()

• drawString()

11
What is Event Handling?

Event Handling is the mechanism used to respond to user interactions such as:

• Mouse click

• Key press
• Button click

• Window closing

In GUI applications, everything happens based on events, so event handling is a core concept.

Event Delegation Model

Java uses the Event Delegation Model, where:

• The component does not handle the event itself

• It delegates the task to a listener object

Three Main Parts:


1. Event Source

o The object that generates the event

o Examples: Button, TextField, Frame

2. Event Object

o Contains information about the event

o Example: ActionEvent, MouseEvent, KeyEvent

3. Event Listener
o Interface that handles the event

o Must be implemented by the programmer

Common Event Listeners

1. ActionListener

• Handles button clicks, menu selections

• Method:

public void actionPerformed(ActionEvent e)


2. MouseListener

12
• Handles mouse events

Methods:

• mouseClicked()

• mousePressed()
• mouseReleased()

• mouseEntered()

• mouseExited()

3. KeyListener

• Handles keyboard input

Methods:

• keyPressed()

• keyReleased()
• keyTyped()

Steps in Event Handling

Step 1: Implement Listener

• Create a class and implement listener interface

Step 2: Register Listener

• Attach listener to event source using method like:

[Link](this);
Step 3: Override Methods

• Provide implementation of listener methods

Swing in Java

What is Swing?

Swing is a part of the [Link] package used to build modern GUI applications in Java. It is an
improved version of AWT and provides a rich, flexible, and platform-independent way to design
user interfaces.

• Built on top of AWT


• Uses lightweight components (written in pure Java)
13
• Provides advanced UI controls and customization

Key Features of Swing (Detailed)

1. Platform Independent

• Written entirely in Java


• Does not depend on OS-native components

• Same look and behavior across all platforms

2. Lightweight Components

• Components do not rely on OS (no native peers)

• Faster and more efficient for complex GUIs

• Examples:

o JButton, JLabel, JTextField

3. Rich Set of Components


Swing provides a wide variety of advanced components:

• Basic: JButton, JLabel, JTextField

• Advanced:

o JTable (tables)

o JTree (hierarchical data)

o JTabbedPane (tabs)

o JScrollPane (scrolling)
4. Better Look and Feel (L&F)

• Supports pluggable look and feel

• UI can be changed dynamically

Examples:

• Windows style

• Nimbus style

• Metal (default)
5. MVC Architecture

14
Swing follows Model-View-Controller (MVC) pattern:

• Model → Data

• View → UI (how it looks)

• Controller → User interaction


This separation makes:

• Code reusable

• Easy to maintain

6. Customizable Components

• You can modify:

o Colors

o Fonts

o Borders
o Icons

Makes UI more attractive and user-friendly

7. Event Handling Support

• Uses event delegation model (same as AWT)

• Works with listeners like:

o ActionListener

o MouseListener
8. Containers in Swing

Swing provides different containers:

• Top-Level Containers:

o JFrame

o JApplet

15
Common Swing Components

Component Purpose

JButton Button

JLabel Display text/image

JTextField Single-line input

JTextArea Multi-line input

JCheckBox Multiple selection

JRadioButton Single selection

JComboBox Dropdown list

Advantages of Swing

• Platform-independent

• Rich and flexible UI

• More components than AWT

• Easy customization

• Suitable for complex applications


Disadvantages of Swing

• Slightly slower than AWT (in some cases)

• More complex to learn

• Requires more memory

16
Difference between AWT vs SWING

Containers in Java

Containers hold and organize components.


Types:

• Top-level containers – Frame, Dialog

• Intermediate containers – Panel

Panes in Swing

• Used to organize GUI components.


Types:

• Content Pane

• Scroll Pane
• Split Pane

• Tabbed Pane

Frames

• A Frame is a top-level window.

Example:
• Frame (AWT)

17
• JFrame (Swing)

Dialog Boxes

• Used for user interaction (input/output).

Types:
• Message Dialog

• Confirm Dialog

• Input Dialog

Example: JOptionPane

Working with Image Controls

• Swing provides image controls using:

Components:

• ImageIcon
• JLabel (to display images)

• JButton (with image icon)

18

You might also like