You are on page 1of 21

AWT

(Abstract Window Toolkit)


BY NEHA KUMARI
INTRODUCTION
Abstract Window Toolkit (AWT) is an API to develop GUI or
Window based applications in Java.
A Graphical User Interface (GUI) is built of graphical elements
called component. AWT provides various components such as buttons, labels, text fields,
checkboxes etc. used for creating GUI elements for Java applications.
Java AWT components are platform dependent that is
components are displayed according to the view of operating system.
The Container class is a subclass of component. Container
class used to display non-container class. AWT provides containers like Panels, Frames
and Dialogues to organize and group components in the application.
In Java GUI can be design using same predefined classes. All
these classes are defined in java.awt package.
java.awt class
The AWT provides container and non-container component classes.

Java.awt

Container classes Non-Container classes

Panel Window Label TextField Button Checkbox Choice List Scrollbar

Applet Frame Dialog


Container Class
 Window: The Window is a top-level container that represents a graphical
window or dialog box. Window class
extends the container class, which
means it can contain other components such as buttons, labels
and text fields.
 Frame: The Frame is the container that contains title bar and border and can
menu bars. It can have other components like button,
text field etc.
 Panel: The Panel is the container that doesn’t contain title bar and menu bars,
It is a light weight container that can be used grouping other
components together within a window or a frame. It can have
other components like button, text field etc.
 Dialog: It represents a rectangular area where application can draw something
or can receive inputs created by user.
Note: Before adding the components that make up a user interface, the programmer must create a container.
Creating the Frame
Create a subclass of Frame

Override any of the standard window methods, such as init(), start(), and paint()

Implement the windowClosing() method of the windowlistener interface.


Calling setVisible(false) when the window is closed.

Once you have defined a Frame subclass, you can create an object of that class. But it
will note be initially visible when created, the window is given a default height and
width.

You can set the size of the window explicity by calling the following method
Void setSize(int height, int width);
Void setSize(dimension d);
Frame Methods
- used to set display user defined message on title bar.
 setTitle(String)

- used to set background color.


 setBackground(color)

- used to set foreground or text color.


 setForground(color)

- set the width and height for frame.


 setSize(dimension)

- Set the frame is visible or not.


 setVisible(boolean)

- Used to set any layout to the frame.


 setLayout()

- Add component to the frame.


 Add(component)
Event Handling
Event : Event Source :
An event is an Object that describes a A source is an object that generates an
state change in a Source. event. Some general Event sources
are,
Some of the activities that cause event
to be generated are:
• Pressing a Button.  Buttons
• Entering a character through Key  CheckBox
Board.
 List
• Selecting an item from a list etc.
 Text items
EVENT HANDLING
Here is a general form for adding a listener to an event source :
public void addTypeListner(TypeEvent e)
Here,
type is the name of the event.
e is the reference of the event listener.

Event Listener
A Listener is an object that is notified when an event occurs.
For example:
mouseMotionaListner interface define two events:
When mouse is dragged.
When mouse is moved.
For implementing event listener we have to import the following statements:
import java.awt.event.*;
HOW TO USE EVENTS
 Components defined in the AWT generate AWTEvents.

 Identify which events you wish to listen for (some components generate more
than one type of event)

 Identify the Listener class which Listens for the event

 Once we have identified the Listener interface, implement the Listener


interface.

 Our listener class must register with the component to received events

 Call the addXXXListener method where XXX is the Event type.


AWT EVENT
The following is a list of events in the java.awt.event package:

 ActionEvent - Action has occurred (e.g.


Button pressed)
 AdjustmentEvent - “Adjustable” Component changed
 ComponentEvent - Component Changed
 ContainerEvent - Container changed (add or remove)
 FocusEvent - Focus Changed
 ItemEvent - Item selected or deselected
 MouseEvent - Mouse event
 KeyEvent - Keyboard event
 TextEvent - Text changed evets
 WindowEvent - Window related events
LISTNER INERFACE
The following events have Listeners associated with them:

 ActionEvent - ActionListener
 AdjustmentEvent - AdjustmentListener
 ComponentEvent - ComponentListener
 ContainerEvent - ContainerListener
 FocusEvent - FocusListener
 ItemEvent - ItemListener
 MouseEvent - MouseListener
 KeyEvent - KeyListener
 TextEvent - TextListener
 WindowEvent - WindowListener
ActionEvent
The ActionEvent is generated when button is clicked or the item of a list is double
clicked.

To write an Action Listener, follow the steps given below:

First declare an event handler class and specify that the class either implements an
ActionListner interface or extends a class that implements an ActionListener
interface.
For example
Public class MyClass implements ActionListener {
//abstract methods of ActionListner interface
}
ActionEvent Listener
Register an instance of the event handler class as a listener on one or more
components.
For example:
someComponent.addActionListner(instanceOfMyClass)
Include code that implements the methods in listener interface.

Public void actionPerformed(ActionEvent e){


If(e.getSource()==someComponent(){
//code that reacts to the action
}
}
KeyEvent
On entering the character from any source generates the key event. This interface
has 3 methods. These are

Public void keyEvent(keyEvent ae){


//active on typing a code .....
}
Public void keyPressed(KeyEvent ae){
//active on pressing a key…….
}
Public void keyPressed(KeyEvent ae){
//active on pressing a key .....
}
Mouse Event
• This event indicates a mouse action occurred in a component.
• This class has 5 interface method as follows:
Public void mousePressed(MouseEvent e) {…….}
Called just after the user presses a mouse button while the cursor is over the
listened-to component.
Public void mouseReleased(MouseEvent e) {…….}
Called just after the user releases a mouse button after a mouse press over the
listened-to component
Public void mouseClicked(MouseEvent e) {……..}
Called just after the user clicks the listened-to component.
Public void mouseEntered(MouseEvent e) {……}
Called just after the cursor enters the bounds of the listened-to component.
Public void mouseExcited(MouseEvent e) {…….}
Called just after the cursor exits the bounds of the listened-to component.
MouseMotionEvent
• The interface MouseMotionEvent indicates a mouse action occurred in a component.
• This low level event is generated by a component object when mouse is dragged or
moved.
• This class provides 2 interface methods:

mouseDragged Method:
executed when mouse is dragged over the listened-to
component..

mouseMoved Method:
execute when mouse is moved over the listened-to
component...
FocusEvent
This class provide two interface methods:

focusGained:
Called just after the listened-to component gets the
focus.

focusLost:
Called just after the listened-to component Loses the
focus.
WindowEvent Listener
This class provide 8 interface methods:

windowOpened:
Called just after the listened-to window has been shown for the first time.
windowClosing:
Called in response to a user request for the listened-to window to be
closed. To actually close the window, the listener should invoke the windowsdispose or
setVisible(false) method.
windowClosed:
Called just after the listened-to window has closed.
windowIconified:
Called just after the listened-to window is iconified.
windowDeiconified:
Called just after the listened-to window is deiconified.
WindowEvent Class
• windowActivated and windowDeactivated:

Called just after the listened-to window is activated or deactivated,


respectively. These methods are not send to windows that are not frames
or dialogs.

We use windowGainedFocus and windowLostFocus methods to


determine when a window gains or loses the focus.
ItemEvent Class
 This class provide one interface method:

itemStateChanged:
Called just after a state change in the listened-to component.
All Content Written By Neha Kumari

Thank You….

You might also like