SlideShare a Scribd company logo
1 of 46
EventHandling In Java……..
Some common term used in EventHandling Event :           An event is an Object that describes a                state change in a Source.      * Some of the activities that causes event to be generated are :                    *Pressing a Button.                     *Entering a character through                              Key Board.                      *Selecting an item form a list etc.
Some Common term Conti.. Event Source :          A source is an object that generates an event.    Some general Event Sources are:                       #Button, CheckBox                       #List, MenusItem                       #Window, TextItems Etc… Here is a general form for adding a listener to an event Source :           public void addTypeListener(TypeEvent e)         * Type is the name of the event.          * e is the reference of the event listener.
Some common terms Conti… Event Listener :               A Listener is an object that is notified when an event occurs.              For example : MouseMotionListener interface define two events:                  *When mouse is dragged.                   * When mouse is moved.
Some common terms Conti… For implementing event listener we have to import the following Statement:                       import java.awt.event.*;
Event Handling There are a number of event class provided by java :   But we’ll discuss today only 7 classes, namely:               *ActionEvent                *KeyEvent                *MouseEvent                *MouseMotionEvent                *FocusEvent                *WindowEvent                 *ItemEvent
Action Event Class Action listeners are probably the easiest — and most common — event handlers to implement. To write an Action Listener, follow the steps given below: Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface.       For example:                      public class MyClass implements ActionListener {                             }
Actionevent Listener conti….. Register an instance of the event handler class as a listener on one or more components.          For example:  someComponent.addActionListener(instanceOfMyClass);  Include code that implements the methods in listener interface.           For example:                        public void actionPerformed(ActionEvent e)                                      {                                              if(e.getSource()==someComponent)                                         //code that reacts to the action...                                      }
Example of Action Listener public class changePanel implements ActionListener{ //Initializing jf,jb,jl1,jl2,jp1,jp2… changePanel(){ jf=new JFrame("Presentation"); jb=new JButton("click");         jl1=new JLabel("1st PANEL");         jl2=new JLabel("2nd PANEL");         jp1=new JPanel();         jp2=new JPanel(); jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY); jf.setLayout(new FlowLayout()); jf.add(jb);         jp1.setSize(200,200); jf.add(jp1);         jp1.add(jl1);         jp2.add(jl2); jf.setVisible(true);
Conti…. jb.addActionListener(this);     }     public void actionPerformed(ActionEventae){         if(ae.getSource()==jb){            jp1.setVisible(false); jf.add(jp2);             jp2.add(jl2);              }     } }
Output Before clicking the button
Output after clicking the button
Key Event Class This class has 3 methods in Listener interface Namely:        public void keyTyped(KeyEventae)            {//active on typing a code…..}        public void keyPressed(KeyEventae)         {//active on pressing a key……}         public void keyReleased(KeyEventae)          {//active on realesing a key…..}
Example….. Package presentation; import java.awt.*; Import java.awt.event.*; Public class keyEventDemo implements KeyListeners{ //initialization occur of jf,jlabel,jText; //memory allocation to them jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY);
Conti…. jf.setLayout(null);         jt1.setBounds(400,200,180,30); jf.add(jt1);         jl1.setBounds(100,200,200,30); jf.add(jl         jt1.addKeyListener(this); jf.setVisible(true);     } public void keyTyped(KeyEvent e) {         jl1.setText("key is typed");     }
Conti…  public void keyPressed(KeyEvent e) {     jl1.setText("key is pressed");     }     public void keyReleased(KeyEvent e) {         jl1.setText("key is relesed");     } }
Output of the example is..No interface method is active yet
Output after typing “n”….
Output after releasing “n”…
Output after pressing shift..
Mouse Event Class This class has 5 interface method as follows : public void mouseClicked(MouseEvent e) {…} Called just after the user clicks the listened-to component.     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 mouseEntered(MouseEvent e) {…….} Called just after the cursor enters the bounds of the listened-to component.     public void mouseExited(MouseEvent e) {……..} Called just after the cursor exits the bounds of the listened-to component.
Example of Mouse Listener.. package presentation; Import java.awt.*; Import java.awt.event.*; public class changePanel implements MouseListener{ //initialization of varibles occur //memory is allocated
jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY); jf.setLayout(null);         jt1.setBounds(300,150,180,30); jf.add(jt1);         jl1.setBounds(100,150,200,30); jf.add(jl1); jf.setVisible(true);         jt1.addMouseListener(this); } public void mouseClicked(MouseEvent e) {        jl1.setText("Mouse is clicked");     }     public void mousePressed(MouseEvent e) {        jl1.setText("Mouse is Pressed");     }
Example conti…..      public void mouseReleased(MouseEvent e) {          jl1.setText("Mouse is Released");     }     public void mouseEntered(MouseEvent e) {      jl1.setText("Mouse is Released");     }     public void mouseExited(MouseEvent e) {      jl1.setText("Mouse is Exited");     } }
Output of the example..When no interface method is called..
Output of the example …when  mouse is entered in the text field…..
Output of the example …when  mouse is Exited in the text field…..
Output of the example …when  mouse is Clicked in the text field…..
Output of the example …when  mouse is Pressed in the text field…..
Output of the example …when  mouse is Released in the text field…..
MouseMotionEvent Class This class provides  2 interface methods:           *mouseDragged Method:                  executed when mouse is dragged over the listened-to component..        *mouseMoved Method:              executed when mouse is moved over the listened-to component…
Example …. Package presentation; import java.awt.*; Import java.awt.event.*; Public class mouseMotionDemo implements MouseMotionListeners{ //initialization occur of jf,jlabel,jText; //memory allocation to them jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY);
jf.setLayout(null);         jt1.setBounds(300,150,180,30); jf.add(jt1);         jl1.setBounds(100,150,200,30); jf.add(jl1); jf.setVisible(true);         jt1.addMouseMotionListener(this); }  public void mouseDragged(MouseEvent e) {        jl1.setText("Mouse is dragged");     }     public void mouseMoved(MouseEvent e) {        jl1.setText(“ Mouse is Moved");     }}
Output of Example
Output of Example
FocusEvent Listener Class 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.
Example package presentation5; import java.awt.*; import java.awt.event.*;  public class FocusListenertest extends Frame implements FocusListener     {   //initialization occur…      public FocusListenertest()     {      //Memory allocation          add(b1=new Button ("First"),"South");          add(b2=new Button ("Second"),"North"); 	add(l);          b1.addFocusListener(this);          b2.addFocusListener(this); setSize(200,200);
Example conti… } public void focusGained(FocusEventfe)  {          if(fe.getSource()==b1)         { l.setText("focus gained of first & Lost of second"); }  public void focusLost(FocusEventfe) {       if(fe.getSource()==b1) {	 l.setText("focus Lost of First & Gained of Second "); }
Output of the Example..When Button  First is pressed…
Output of the Example..When second is pressed…
WindowEvent Class 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 window's dispose or setVisible(false) method.
WindowEvent Class conti… windowClosed:              Called just after the listened-to window has closed.  windowIconified:             Called just after the listened-to window is iconified . windowDeicoified:  Called just after the listened-to window is deiconified.
WindowEvent Class conti… windowActivated and windowDeactivated :            Called just after the listened-to window is activated or deactivated, respectively. These methods are not sent to windows that are not frames or dialogs. For this reason, we prefer the 1.4 windowGainedFocus andwindowLostFocus 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.
Example… //where initialization occurscheckbox.addItemListener(this); ...   public void itemStateChanged(ItemEvent e)  {  if (e.getStateChange() == ItemEvent.SELECTED) { label.setVisible(true); ... }  else  { label.setVisible(false);  } }
  So…..  this was a little Knowledge about the  concept of Event Handling…….       Thank You….

More Related Content

What's hot

Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
Tech_MX
 

What's hot (20)

Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Event handling
Event handlingEvent handling
Event handling
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Java swing
Java swingJava swing
Java swing
 
Exception Handling in Java
Exception Handling in JavaException Handling in Java
Exception Handling in Java
 
Wrapper classes
Wrapper classes Wrapper classes
Wrapper classes
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
Java collections concept
Java collections conceptJava collections concept
Java collections concept
 
Swing
SwingSwing
Swing
 
Java threads
Java threadsJava threads
Java threads
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Applets
AppletsApplets
Applets
 
Applets in java
Applets in javaApplets in java
Applets in java
 
Operators in java
Operators in javaOperators in java
Operators in java
 
MULTI THREADING IN JAVA
MULTI THREADING IN JAVAMULTI THREADING IN JAVA
MULTI THREADING IN JAVA
 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
 
JVM
JVMJVM
JVM
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 

Viewers also liked (15)

Bluetooth wi fi wi max 2, 3
Bluetooth wi fi wi max 2, 3Bluetooth wi fi wi max 2, 3
Bluetooth wi fi wi max 2, 3
 
Bluetooth technology
Bluetooth technologyBluetooth technology
Bluetooth technology
 
software project management Waterfall model
software project management Waterfall modelsoftware project management Waterfall model
software project management Waterfall model
 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronization
 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in java
 
Bluetooth - Overview
Bluetooth - OverviewBluetooth - Overview
Bluetooth - Overview
 
Access modifiers in java
Access modifiers in javaAccess modifiers in java
Access modifiers in java
 
Bluetooth
BluetoothBluetooth
Bluetooth
 
Awt
AwtAwt
Awt
 
Wi-Fi vs Bluetooth
Wi-Fi vs BluetoothWi-Fi vs Bluetooth
Wi-Fi vs Bluetooth
 
Waterfall Model
Waterfall ModelWaterfall Model
Waterfall Model
 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiers
 
Waterfall model ppt final
Waterfall model ppt  finalWaterfall model ppt  final
Waterfall model ppt final
 
Waterfall model
Waterfall modelWaterfall model
Waterfall model
 
Wi-Fi Technology
Wi-Fi TechnologyWi-Fi Technology
Wi-Fi Technology
 

Similar to Event Handling in java

Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
PRN USM
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
sotlsoc
 

Similar to Event Handling in java (20)

Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
What is Event
What is EventWhat is Event
What is Event
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
Swing
SwingSwing
Swing
 
Lecture8 oopj
Lecture8 oopjLecture8 oopj
Lecture8 oopj
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
 
Androd Listeners
Androd ListenersAndrod Listeners
Androd Listeners
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handling
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
09events
09events09events
09events
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Synapseindia dotnet development chapter 14 event-driven programming
Synapseindia dotnet development  chapter 14 event-driven programmingSynapseindia dotnet development  chapter 14 event-driven programming
Synapseindia dotnet development chapter 14 event-driven programming
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 

Recently uploaded

1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
QucHHunhnh
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
SanaAli374401
 

Recently uploaded (20)

Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"Mattingly "AI & Prompt Design: The Basics of Prompt Design"
Mattingly "AI & Prompt Design: The Basics of Prompt Design"
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
ICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptxICT Role in 21st Century Education & its Challenges.pptx
ICT Role in 21st Century Education & its Challenges.pptx
 
Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
Measures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and ModeMeasures of Central Tendency: Mean, Median and Mode
Measures of Central Tendency: Mean, Median and Mode
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Measures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SDMeasures of Dispersion and Variability: Range, QD, AD and SD
Measures of Dispersion and Variability: Range, QD, AD and SD
 
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17  How to Extend Models Using Mixin ClassesMixin Classes in Odoo 17  How to Extend Models Using Mixin Classes
Mixin Classes in Odoo 17 How to Extend Models Using Mixin Classes
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
microwave assisted reaction. General introduction
microwave assisted reaction. General introductionmicrowave assisted reaction. General introduction
microwave assisted reaction. General introduction
 

Event Handling in java

  • 2. Some common term used in EventHandling Event : An event is an Object that describes a state change in a Source. * Some of the activities that causes event to be generated are : *Pressing a Button. *Entering a character through Key Board. *Selecting an item form a list etc.
  • 3. Some Common term Conti.. Event Source : A source is an object that generates an event. Some general Event Sources are: #Button, CheckBox #List, MenusItem #Window, TextItems Etc… Here is a general form for adding a listener to an event Source : public void addTypeListener(TypeEvent e) * Type is the name of the event. * e is the reference of the event listener.
  • 4. Some common terms Conti… Event Listener : A Listener is an object that is notified when an event occurs. For example : MouseMotionListener interface define two events: *When mouse is dragged. * When mouse is moved.
  • 5. Some common terms Conti… For implementing event listener we have to import the following Statement: import java.awt.event.*;
  • 6. Event Handling There are a number of event class provided by java : But we’ll discuss today only 7 classes, namely: *ActionEvent *KeyEvent *MouseEvent *MouseMotionEvent *FocusEvent *WindowEvent *ItemEvent
  • 7. Action Event Class Action listeners are probably the easiest — and most common — event handlers to implement. To write an Action Listener, follow the steps given below: Declare an event handler class and specify that the class either implements an ActionListener interface or extends a class that implements an ActionListener interface. For example: public class MyClass implements ActionListener { }
  • 8. Actionevent Listener conti….. Register an instance of the event handler class as a listener on one or more components. For example: someComponent.addActionListener(instanceOfMyClass); Include code that implements the methods in listener interface. For example: public void actionPerformed(ActionEvent e) { if(e.getSource()==someComponent) //code that reacts to the action... }
  • 9. Example of Action Listener public class changePanel implements ActionListener{ //Initializing jf,jb,jl1,jl2,jp1,jp2… changePanel(){ jf=new JFrame("Presentation"); jb=new JButton("click"); jl1=new JLabel("1st PANEL"); jl2=new JLabel("2nd PANEL"); jp1=new JPanel(); jp2=new JPanel(); jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY); jf.setLayout(new FlowLayout()); jf.add(jb); jp1.setSize(200,200); jf.add(jp1); jp1.add(jl1); jp2.add(jl2); jf.setVisible(true);
  • 10. Conti…. jb.addActionListener(this); } public void actionPerformed(ActionEventae){ if(ae.getSource()==jb){ jp1.setVisible(false); jf.add(jp2); jp2.add(jl2); } } }
  • 12. Output after clicking the button
  • 13. Key Event Class This class has 3 methods in Listener interface Namely: public void keyTyped(KeyEventae) {//active on typing a code…..} public void keyPressed(KeyEventae) {//active on pressing a key……} public void keyReleased(KeyEventae) {//active on realesing a key…..}
  • 14. Example….. Package presentation; import java.awt.*; Import java.awt.event.*; Public class keyEventDemo implements KeyListeners{ //initialization occur of jf,jlabel,jText; //memory allocation to them jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY);
  • 15. Conti…. jf.setLayout(null); jt1.setBounds(400,200,180,30); jf.add(jt1); jl1.setBounds(100,200,200,30); jf.add(jl jt1.addKeyListener(this); jf.setVisible(true); } public void keyTyped(KeyEvent e) { jl1.setText("key is typed"); }
  • 16. Conti… public void keyPressed(KeyEvent e) { jl1.setText("key is pressed"); } public void keyReleased(KeyEvent e) { jl1.setText("key is relesed"); } }
  • 17. Output of the example is..No interface method is active yet
  • 18. Output after typing “n”….
  • 21. Mouse Event Class This class has 5 interface method as follows : public void mouseClicked(MouseEvent e) {…} Called just after the user clicks the listened-to component. 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 mouseEntered(MouseEvent e) {…….} Called just after the cursor enters the bounds of the listened-to component. public void mouseExited(MouseEvent e) {……..} Called just after the cursor exits the bounds of the listened-to component.
  • 22. Example of Mouse Listener.. package presentation; Import java.awt.*; Import java.awt.event.*; public class changePanel implements MouseListener{ //initialization of varibles occur //memory is allocated
  • 23. jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY); jf.setLayout(null); jt1.setBounds(300,150,180,30); jf.add(jt1); jl1.setBounds(100,150,200,30); jf.add(jl1); jf.setVisible(true); jt1.addMouseListener(this); } public void mouseClicked(MouseEvent e) { jl1.setText("Mouse is clicked"); } public void mousePressed(MouseEvent e) { jl1.setText("Mouse is Pressed"); }
  • 24. Example conti….. public void mouseReleased(MouseEvent e) { jl1.setText("Mouse is Released"); } public void mouseEntered(MouseEvent e) { jl1.setText("Mouse is Released"); } public void mouseExited(MouseEvent e) { jl1.setText("Mouse is Exited"); } }
  • 25. Output of the example..When no interface method is called..
  • 26. Output of the example …when mouse is entered in the text field…..
  • 27. Output of the example …when mouse is Exited in the text field…..
  • 28. Output of the example …when mouse is Clicked in the text field…..
  • 29. Output of the example …when mouse is Pressed in the text field…..
  • 30. Output of the example …when mouse is Released in the text field…..
  • 31. MouseMotionEvent Class This class provides 2 interface methods: *mouseDragged Method: executed when mouse is dragged over the listened-to component.. *mouseMoved Method: executed when mouse is moved over the listened-to component…
  • 32. Example …. Package presentation; import java.awt.*; Import java.awt.event.*; Public class mouseMotionDemo implements MouseMotionListeners{ //initialization occur of jf,jlabel,jText; //memory allocation to them jf.setSize(500,500); jf.setBackground(Color.LIGHT_GRAY);
  • 33. jf.setLayout(null); jt1.setBounds(300,150,180,30); jf.add(jt1); jl1.setBounds(100,150,200,30); jf.add(jl1); jf.setVisible(true); jt1.addMouseMotionListener(this); } public void mouseDragged(MouseEvent e) { jl1.setText("Mouse is dragged"); } public void mouseMoved(MouseEvent e) { jl1.setText(“ Mouse is Moved"); }}
  • 36. FocusEvent Listener Class 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.
  • 37. Example package presentation5; import java.awt.*; import java.awt.event.*; public class FocusListenertest extends Frame implements FocusListener { //initialization occur… public FocusListenertest() { //Memory allocation add(b1=new Button ("First"),"South"); add(b2=new Button ("Second"),"North"); add(l); b1.addFocusListener(this); b2.addFocusListener(this); setSize(200,200);
  • 38. Example conti… } public void focusGained(FocusEventfe) { if(fe.getSource()==b1) { l.setText("focus gained of first & Lost of second"); } public void focusLost(FocusEventfe) { if(fe.getSource()==b1) { l.setText("focus Lost of First & Gained of Second "); }
  • 39. Output of the Example..When Button First is pressed…
  • 40. Output of the Example..When second is pressed…
  • 41. WindowEvent Class 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 window's dispose or setVisible(false) method.
  • 42. WindowEvent Class conti… windowClosed: Called just after the listened-to window has closed. windowIconified: Called just after the listened-to window is iconified . windowDeicoified: Called just after the listened-to window is deiconified.
  • 43. WindowEvent Class conti… windowActivated and windowDeactivated : Called just after the listened-to window is activated or deactivated, respectively. These methods are not sent to windows that are not frames or dialogs. For this reason, we prefer the 1.4 windowGainedFocus andwindowLostFocus methods to determine when a window gains or loses the focus.
  • 44. ItemEvent Class This class provide one interface method: itemStateChanged: Called just after a state change in the listened-to component.
  • 45. Example… //where initialization occurscheckbox.addItemListener(this); ... public void itemStateChanged(ItemEvent e) { if (e.getStateChange() == ItemEvent.SELECTED) { label.setVisible(true); ... } else { label.setVisible(false); } }
  • 46. So….. this was a little Knowledge about the concept of Event Handling……. Thank You….