Unit - 5
Unit - 5
Unit - 5
Dr Jayapal
mrcetjp@gmail.com
ABSTRACT WINDOW TOOLKIT (AWT)
Java AWT (Abstract Window Toolkit) is an API to develop GUI
or window-based application in java. Java AWT components
are platform-dependent i.e. components are displayed
according to the view of operating system. AWT is heavyweight
i.e. its components uses the resources of system. The Abstract
Window Toolkit(AWT) support for applets. The AWT contains
numerous classes and methods that allow you to create and
manage windows.
The java.awt package provides classes for AWT api such as
TextField, Label, TextArea, RadioButton, CheckBox, Choice,
List etc.
Dr Jayapal
mrcetjp@gmail.com
Java AWT Hierarchy
Dr Jayapal
mrcetjp@gmail.com
AWT Classes
The AWT classes are contained in the java.awt package. It is one of
Java's largest packages.
Class Description
AWTEvent Encapsulates AWT events.
AWTEventMulticaster Dispatches events to multiple
listeners.
BorderLayout Border layouts use five
components: North, South, East,
West, and Center.
CardLayout Card layouts emulate index cards.
Only the one on top is showing.
Checkbox Creates a check box control.
CheckboxGroup Creates a group of check box
controls.
CheckboxMenuItem Creates an on/off menu item.
Choice Creates a pop-up list.
Color Manages colors in a portable,
Drplatform-independent
Jayapal fashion.
mrcetjp@gmail.com
Class Description
Component An abstract superclass for various AWT
components.
Container A subclass of Component that can hold
other components.
Cursor Encapsulates a bitmapped cursor.
Dialog Creates a dialog window.
Dimension Specifies the dimensions of an object.
The width is stored in
width , and the height is stored in height
Event Encapsulates events.
FlowLayout The flow layout manager. Flow layout
positions components left to right, top
to bottom.
Frame Creates a standard window that has a
title bar, resize corners, and a menu bar.
Graphics Encapsulates
Dr Jayapal the graphics context.
mrcetjp@gmail.com
Components:All the elements like the button, text fields, scroll bars, etc. are
called components. In order to place every component in a particular position
on a screen, we need to add them to a container.
Dr Jayapal
mrcetjp@gmail.com
Window: window is the container that have no borders and menu bars.
frame, dialog or another window is used for creating a window. Instance of
Window class is used to create this container.
Panel:Panel is the container that doesn't contain title bar, border or menu
bar. It is generic container for holding the components. It can have other
components like button, text field etc. An instance of Panel class creates a
container, in which components can be added.
Frame:Frame is the container that contain title bar and border and can have
menu bars. It can have other components like button, text field, scrollbar etc.
Frame is most widely used container while developing an AWT application.
Dialog:Dialog boxes are graphical components that are usually used to display
errors or give some other information to the user.
Dr Jayapal
mrcetjp@gmail.com
There are two ways to create a GUI using Frame in AWT.
1. By extending Frame class (inheritance)
2. By creating the object of Frame class (association)
Dr Jayapal
mrcetjp@gmail.com
There are two ways to create a GUI using Frame in AWT.
1. By extending Frame class (inheritance)
2. By creating the object of Frame class (association)
Control Fundamentals
The AWT supports the following types of controls:
1. Labels
2. Push buttons
3. Check boxes
4. Choice lists
5. Lists
6. Scroll bars
7. Text editing
Dr Jayapal
mrcetjp@gmail.com
SWINGS
Swing is a set of classes that provides more powerful and flexible
components than are possible with the AWT. Swing is a GUI
widget toolkit for Java. It is part of Oracle's 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, JColorChooser etc.
Dr Jayapal
mrcetjp@gmail.com
Differences between AWT and Swing
AWT Swing
Dr Jayapal
mrcetjp@gmail.com
Differences between AWT and Swing
AWT Swing
This feature is not supported in We can have different look and feel in Swing.
AWT.
Using AWT, you have to
implement a lot ofthings Swing has them built in.
yourself.
Dr Jayapal
mrcetjp@gmail.com
JApplet
Fundamental to Swing is the JApplet class, which extends Applet.
Applets that use Swing must be subclasses of JApplet. JApplet is
rich with functionality that is not foundin Applet.
The content pane can be obtained via the method shown here:
Container getContentPane( )
The add( ) method of Container can be used to add a component
to a content pane. Its form is shown here:
void add(comp)
Here, comp is the component to be added to the content pane.
Dr Jayapal
mrcetjp@gmail.com
JFrame
Create an object to JFrame: JFrame ob = new JFrame ("title"); (or)
Create a class as subclass to JFrame class: MyFrame extends
JFrame Create an object to that class : MyFrame ob = new
MyFrame ();
Glass Pane: This is the first pane and is very close to the
monitors screen. Any components to be displayed in the
foreground are attached to this glass pane. To reach this glass
pane, we use getGlassPane () method of JFrame class.
Dr Jayapal
mrcetjp@gmail.com
Root Pane: This pane is below the glass pane. Any components
to be displayed in the background are displayed in this pane.
Root pane and glass pane are used in animations also.
For example, suppose we want to display a flying aeroplane in
the sky. The aeroplane can be displayed as a .gif or .jpg file in the
glass pane where as the blue sky can be displayed in the root
pane in the background. To reach this root pane, we use
getRootPane () method of JFrame class.
Layered Pane: This pane lies below the root pane. When we
want to take several components as a group, we attach them in
the layered pane. We can reach this pane by calling
getLayeredPane () method of JFrame class.
Dr Jayapal
mrcetjp@gmail.com
Border Layout
The Border Layout 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
Dr Jayapal
mrcetjp@gmail.com
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.
Dr Jayapal
mrcetjp@gmail.com
FlowLayout
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.
FlowLayout(int align, int hgap, int vgap): creates a flow
layout with the given alignment and the given horizontal
and vertical gap.
Dr Jayapal
mrcetjp@gmail.com
Java Event classes and Listener interfaces
Event Classes Listener Interfaces
ActionEvent ActionListener
MouseEvent MouseListener and MouseMotionListener
MouseWheelEvent MouseWheelListener
KeyEvent KeyListener
ItemEvent ItemListener
TextEvent TextListener
AdjustmentEvent AdjustmentListener
WindowEvent WindowListener
ComponentEvent ComponentListener
ContainerEvent ContainerListener
FocusEvent FocusListener
Dr Jayapal
mrcetjp@gmail.com
Event Handling can be performed by Registering methods.
For registering the component with the Listener, many classes provide the
registration methods. For example:
1.Button
public void addActionListener(ActionListener a){}
2.MenuItem
public void addActionListener(ActionListener a){}
3.TextField
public void addActionListener(ActionListener a){}
public void addTextListener(TextListener a){}
4.TextArea
public void addTextListener(TextListener a){}
5.Checkbox
public void addItemListener(ItemListener a){}
6.Choice
public void addItemListener(ItemListener a){}
7.List
public void addActionListener(ActionListener a){}
public void addItemListener(ItemListener a){}
Dr Jayapal
mrcetjp@gmail.com
Applets
Applet is a special type of program that is embedded in the webpage to
generate the dynamic content. It runs inside the browser and works at client
side.
Advantage of Applet
•There are many advantages of applet. They are as follows:
It works at client side so less response time.
•It
Secured
•Secured
•It can be executed by browsers running under many plateforms, including
Linux, Windows, Mac Os etc.
Drawback of Applet
•Plugin is required at client browser to execute applet.
Dr Jayapal
mrcetjp@gmail.com