SlideShare a Scribd company logo
1 of 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
Java.awt
Container classes
Panel
Applet
Window
Frame Dialog
Non-Container classes
Label TextField Button Checkbox Choice List Scrollbar
The AWT provides container and non-container component classes.
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);
dimension d = new dimension(400, 400);
Frame Methods
 setTitle(String)
 setBackground(color)
 setForground(color)
 setSize(dimension)
 setVisible(boolean)
 setLayout()
 Add(component)
- used to set display user defined message on title bar.
- used to set background color.
- used to set foreground or text color.
- set the width and height for frame.
- Set the frame is visible or not.
- Used to set any layout to the frame.
- Add component to the frame.
Event Handling
Event :
An event is an Object that describes a
state change in a Source.
Some of the activities that cause event
to be generated are:
• Pressing a Button.
• Entering a character through Key
Board.
• Selecting an item from a list etc.
Event Source :
A source is an object that generates an
event. Some general Event sources
are,
 Buttons
 CheckBox
 List
 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
 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
The following is a list of events in the java.awt.event package:
LISTNER INERFACE
 ActionEvent - ActionListener
 AdjustmentEvent - AdjustmentListener
 ComponentEvent - ComponentListener
 ContainerEvent - ContainerListener
 FocusEvent - FocusListener
 ItemEvent - ItemListener
 MouseEvent - MouseListener
 KeyEvent - KeyListener
 TextEvent - TextListener
 WindowEvent - WindowListener
The following events have Listeners associated with them:
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

More Related Content

Similar to Java Abstract Window Toolkit (AWT) Presentation. 2024

Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01Ankit Dubey
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handlingAmol Gaikwad
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in JavaAyesha Kanwal
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.pptusama537223
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01JONDHLEPOLY
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingPayal Dungarwal
 
Java session11
Java session11Java session11
Java session11Niit Care
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in javaGoogle
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)jammiashok123
 

Similar to Java Abstract Window Toolkit (AWT) Presentation. 2024 (20)

Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
Lecture8 oopj
Lecture8 oopjLecture8 oopj
Lecture8 oopj
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
Androd Listeners
Androd ListenersAndrod Listeners
Androd Listeners
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
AWT information
AWT informationAWT information
AWT information
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
Ajp notes-chapter-01
Ajp notes-chapter-01Ajp notes-chapter-01
Ajp notes-chapter-01
 
Java
JavaJava
Java
 
AWT
AWT AWT
AWT
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
 
Java session11
Java session11Java session11
Java session11
 
JAVA (UNIT 5)
JAVA (UNIT 5)JAVA (UNIT 5)
JAVA (UNIT 5)
 
Event Handling in java
Event Handling in javaEvent Handling in java
Event Handling in java
 
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 Dr Jammi Ashok - Introduction to Java Material (OOPs) Dr Jammi Ashok - Introduction to Java Material (OOPs)
Dr Jammi Ashok - Introduction to Java Material (OOPs)
 

Recently uploaded

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Krashi Coaching
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxOH TEIK BIN
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting DataJhengPantaleon
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13Steve Thomason
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Celine George
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdfSoniaTolstoy
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonJericReyAuditor
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfakmcokerachita
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxRaymartEstabillo3
 

Recently uploaded (20)

Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
Kisan Call Centre - To harness potential of ICT in Agriculture by answer farm...
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Solving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptxSolving Puzzles Benefits Everyone (English).pptx
Solving Puzzles Benefits Everyone (English).pptx
 
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data_Math 4-Q4 Week 5.pptx Steps in Collecting Data
_Math 4-Q4 Week 5.pptx Steps in Collecting Data
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13The Most Excellent Way | 1 Corinthians 13
The Most Excellent Way | 1 Corinthians 13
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
Incoming and Outgoing Shipments in 1 STEP Using Odoo 17
 
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdfBASLIQ CURRENT LOOKBOOK  LOOKBOOK(1) (1).pdf
BASLIQ CURRENT LOOKBOOK LOOKBOOK(1) (1).pdf
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Science lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lessonScience lesson Moon for 4th quarter lesson
Science lesson Moon for 4th quarter lesson
 
Class 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdfClass 11 Legal Studies Ch-1 Concept of State .pdf
Class 11 Legal Studies Ch-1 Concept of State .pdf
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptxEPANDING THE CONTENT OF AN OUTLINE using notes.pptx
EPANDING THE CONTENT OF AN OUTLINE using notes.pptx
 

Java Abstract Window Toolkit (AWT) Presentation. 2024

  • 2. 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.
  • 3. java.awt class Java.awt Container classes Panel Applet Window Frame Dialog Non-Container classes Label TextField Button Checkbox Choice List Scrollbar The AWT provides container and non-container component classes.
  • 4. 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.
  • 5. 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); dimension d = new dimension(400, 400);
  • 6. Frame Methods  setTitle(String)  setBackground(color)  setForground(color)  setSize(dimension)  setVisible(boolean)  setLayout()  Add(component) - used to set display user defined message on title bar. - used to set background color. - used to set foreground or text color. - set the width and height for frame. - Set the frame is visible or not. - Used to set any layout to the frame. - Add component to the frame.
  • 7. Event Handling Event : An event is an Object that describes a state change in a Source. Some of the activities that cause event to be generated are: • Pressing a Button. • Entering a character through Key Board. • Selecting an item from a list etc. Event Source : A source is an object that generates an event. Some general Event sources are,  Buttons  CheckBox  List  Text items
  • 8. 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.*;
  • 9. 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.
  • 10. AWT EVENT  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 The following is a list of events in the java.awt.event package:
  • 11. LISTNER INERFACE  ActionEvent - ActionListener  AdjustmentEvent - AdjustmentListener  ComponentEvent - ComponentListener  ContainerEvent - ContainerListener  FocusEvent - FocusListener  ItemEvent - ItemListener  MouseEvent - MouseListener  KeyEvent - KeyListener  TextEvent - TextListener  WindowEvent - WindowListener The following events have Listeners associated with them:
  • 12. 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 }
  • 13. 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 } }
  • 14. 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 ..... }
  • 15. 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.
  • 16. 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...
  • 17. 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.
  • 18. 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.
  • 19. 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.
  • 20. ItemEvent Class  This class provide one interface method: itemStateChanged: Called just after a state change in the listened-to component.
  • 21. All Content Written By Neha Kumari