0% found this document useful (0 votes)
180 views

Unit 5

This document discusses GUI programming with Java's Abstract Window Toolkit (AWT) and Swing. It covers the AWT class hierarchy and components, differences between AWT and Swing, Swing components and examples, and the lifecycle of Java applets.

Uploaded by

sri devi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
180 views

Unit 5

This document discusses GUI programming with Java's Abstract Window Toolkit (AWT) and Swing. It covers the AWT class hierarchy and components, differences between AWT and Swing, Swing components and examples, and the lifecycle of Java applets.

Uploaded by

sri devi
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 26

Unit-5

GUI Programming
with Java:
AWT and Swing
The AWT Class Hierarchy
Abstract Window Toolkit(AWT)
• The AWT contains numerous classes and
methods that allow you to create and
manage windows.
• Although a common use of the AWT is in
applets, it is also used to create stand-
alone
windows that run in a GUI environment,
such as Windows.
• Today, most Java programs employ user
interfaces based on Swing.

• Because Swing provides richer


implementations than does the AWT of
some common GUI controls, such as
buttons, lists, and check boxes.

• It is easy to jump to the conclusion that


the AWT is no longer important, that it has
been superseded by Swing.
AWT Classes

• The AWT classes are contained in the java.


awt package.
• It is one of Java’s largest packages.
Hierarchy of AWT
Window
• The window is the container that have no
borders and menu bars.
• You must use frame, dialog or another
window for creating a window.
• you will use a subclass of Window called
Frame.
panel
• a Panel is a window that does not contain
a title bar, menu bar, or border.
Frame
• The Frame is the container that contain
title bar and can have menu bars. It can
have other components like button,
textfield etc.
• Frame encapsulates what is commonly
thought of as a “window.” It is a subclass
of Window and has a title bar, menu bar,
borders, and resizing corners.
Here are two of Frame’s constructors:
• Frame( )
• Frame(String title)
Java AWT Example
• To create simple AWT example, you need
a frame.
There are two ways to create a frame in AWT.
• By extending Frame class (inheritance)
• By creating the object of Frame class
(association)
AWT vs SWING
AWT Swing
AWT stands for Abstract Window Toolkit. Swing is a part of Java Foundation Class (JFC).

AWT components are heavy weight. Swing components are light weight.

AWT components are platform dependent so Swing components are platform independent so
there look and feel changes according to OS. there look and feel remains constant.

AWT components are not very good in look and Swing components are better in look and feel as
feel as compared to Swing components. See the compared to AWT. See the button in below
button in below image, its look is not good image, its look is better than button created
as button created using Swing. using AWT.
   
No. Java AWT Java Swing
1) AWT components are platform-dependent. Java swing components
are platform-independent.

2) AWT components are heavyweight. Swing components


are lightweight.
3) AWT doesn't support pluggable look and feel. Swing supports pluggable
look and feel.

4) AWT provides less components than Swing. Swing provides more


powerful components such
as tables, lists, scrollpanes,
colorchooser, tabbedpane etc.

5) 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.
SWINGS
• 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, JColorChooser etc.
SWING Hierarchy
Java Swing Examples

There are two ways to create a frame:


• By creating the object of Frame class
(association)
• By extending Frame class (inheritance)
• We can write the code of swing inside the
main(), constructor or any other method.
Method Description

public void add(Component c) add a component on


another component.

public void setSize(int width,int height) sets size of the


component.

public void setLayout(LayoutManager m) sets the layout manager


for the component.

public void setVisible(boolean b) sets the visibility of the


component. It is by default
false.
Swings Components
• JButton class
• JRadioButton class
• JTextArea class
• JComboBox class
• JTable class
• JColorChooser class
• JProgressBar class
• JSlider class
• Digital Watch
• Graphics in swing
• Displaying image
• Edit menu code for Notepad
• OpenDialog Box
• Notepad
• Puzzle Game
• Pic Puzzle Game
• Tic Tac Toe Game
• BorderLayout
• GridLayout
• FlowLayout
• CardLayout
Applets
Hierarchy of Applet
Life cycle of applet
• init()
• start()
• stop
• Destroy
• init(): Which will be executed whenever an
applet program start loading, it contains the
logic to initiate the applet properties.
• start(): It will be executed whenever the
applet program starts running.
• stop(): Which will be executed whenever the
applet window or browser is minimized.
• destroy(): It will be executed whenever the
applet window or browser is going to be
closed (at the time of destroying the applet
program permanently).
Syntax:

class className extends Applet


{
...... // override lifecycle methods ......
}
Paint() and repaint() methods in
Applet
The difference between paint() and repaint()
• Paint():- The paint() method is called
automatically whenever the window needs to be
refreshed.
• The programmer never calls paint() .
• repaint():-Programmer calls repaint() in order to
obtain a rendering.
• repaint() then again call paint() to service
repaint() method.
• Thus the paint() method conjuction with update()
method makes repaint();
Applet Security Issues
What Applets Can and Cannot Do

• Java applets are loaded on a client when the user visits


a page containing an applet. The security model behind
Java applets has been designed with the goal of
protecting the user from malicious applets.
• Applets are either sandbox applets or privileged applets.
• Sandbox applets are run in a security sandbox that
allows only a set of safe operations.
• Privileged applets can run outside the security sandbox
and have extensive capabilities to access the client.
• Applets that are not signed are restricted to the security
sandbox, and run only if the user accepts the applet.
• Applets that are signed by a certificate from a
recognized certificate authority can either run only in the
sandbox, or can request permission to run outside the
sandbox. In either case, the user must accept the
applet's security certificate, otherwise the applet is
blocked from running.
• It is recommended that you deploy your
applets to a web server, even for testing. To
run applets locally, add the applets to the
exception site list, which is managed from
the Security tab of the Java Control Panel.

Privileged applets
• Privileged applets do not have the security
restrictions that are imposed on sandbox
applets and can run outside the security
sandbox.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy