SlideShare a Scribd company logo
EventHandlingIn Java……..
Some common term used in EventHandlingEvent :          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 HandlingThere 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 ClassAction 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 Listenerpublic 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 ClassThis class has 3 methods in Listener interfaceNamely:       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 themjf.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 ClassThis 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 ClassThis 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 themjf.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 ClassThis 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.
Examplepackage 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 ClassThis  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 ClassThis 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

PPTX
Event handling
PPTX
JAVA AWT
PDF
Bootstrap
PPTX
internet protocol
PPT
B trees in Data Structure
PDF
PPTX
System of Homogeneous and Non-Homogeneous equations ppt nadi.pptx
PPTX
NSS PPT FOR NAAC PEER TEAM VISIT
Event handling
JAVA AWT
Bootstrap
internet protocol
B trees in Data Structure
System of Homogeneous and Non-Homogeneous equations ppt nadi.pptx
NSS PPT FOR NAAC PEER TEAM VISIT

What's hot (20)

PPTX
Event Handling in Java
PPTX
Java swing
PPTX
This keyword in java
PPTX
Strings in Java
PPT
Asp.net.
PPT
Abstract class in java
PPT
Abstract class
PDF
JavaScript - Chapter 12 - Document Object Model
PPT
Applet life cycle
PPTX
Java awt (abstract window toolkit)
PPT
Java interfaces
PPT
Introduction to method overloading & method overriding in java hdm
PPT
9. Input Output in java
PPT
Server Controls of ASP.Net
PPTX
I/O Streams
PPTX
Interface in java
PPTX
Java beans
PPT
Java layoutmanager
PPTX
Presentation on-exception-handling
Event Handling in Java
Java swing
This keyword in java
Strings in Java
Asp.net.
Abstract class in java
Abstract class
JavaScript - Chapter 12 - Document Object Model
Applet life cycle
Java awt (abstract window toolkit)
Java interfaces
Introduction to method overloading & method overriding in java hdm
9. Input Output in java
Server Controls of ASP.Net
I/O Streams
Interface in java
Java beans
Java layoutmanager
Presentation on-exception-handling
Ad

Viewers also liked (15)

DOCX
Bluetooth wi fi wi max 2, 3
PPTX
Bluetooth technology
PPTX
software project management Waterfall model
PPT
Classical problem of synchronization
PPTX
Visibility control in java
PPTX
Bluetooth - Overview
PDF
Access modifiers in java
PPT
Bluetooth
PPTX
PPT
Wi-Fi vs Bluetooth
PPT
PPT
Java access modifiers
PPTX
Waterfall model ppt final
PPTX
Waterfall model
PPTX
Wi-Fi Technology
Bluetooth wi fi wi max 2, 3
Bluetooth technology
software project management Waterfall model
Classical problem of synchronization
Visibility control in java
Bluetooth - Overview
Access modifiers in java
Bluetooth
Wi-Fi vs Bluetooth
Java access modifiers
Waterfall model ppt final
Waterfall model
Wi-Fi Technology
Ad

Similar to Event Handling in java (20)

PPT
Graphical User Interface (GUI) - 2
PPTX
Chapter 11.5
PPTX
What is Event
PPT
event handling new.ppt
PDF
Swing
DOCX
Lecture8 oopj
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
PPTX
Java Abstract Window Toolkit (AWT) Presentation. 2024
PPTX
Androd Listeners
PDF
java-programming GUI- Event Handling.pdf
PPTX
Event Handling in JAVA
PDF
OOPS unit V JavaFX Event Handling Controls and Components
PPTX
EventHandling in object oriented programming
PPTX
Advance Java Programming(CM5I) Event handling
PDF
Unit-3 event handling
PPTX
tL20 event handling
PDF
Ajp notes-chapter-03
PPT
JAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPT
PPT
java - topic (event handling notes )1.ppt
Graphical User Interface (GUI) - 2
Chapter 11.5
What is Event
event handling new.ppt
Swing
Lecture8 oopj
Java Abstract Window Toolkit (AWT) Presentation. 2024
Java Abstract Window Toolkit (AWT) Presentation. 2024
Androd Listeners
java-programming GUI- Event Handling.pdf
Event Handling in JAVA
OOPS unit V JavaFX Event Handling Controls and Components
EventHandling in object oriented programming
Advance Java Programming(CM5I) Event handling
Unit-3 event handling
tL20 event handling
Ajp notes-chapter-03
JAVA (CHAPTER 1 EXCEPTION HANDLING) NOTES -PPT
java - topic (event handling notes )1.ppt

Recently uploaded (20)

PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
Module 3: Health Systems Tutorial Slides S2 2025
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
Business Ethics Teaching Materials for college
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
PPTX
How to Manage Starshipit in Odoo 18 - Odoo Slides
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PDF
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PDF
English Language Teaching from Post-.pdf
DOCX
UPPER GASTRO INTESTINAL DISORDER.docx
PDF
The Final Stretch: How to Release a Game and Not Die in the Process.
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
Cell Structure & Organelles in detailed.
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
O7-L3 Supply Chain Operations - ICLT Program
Module 3: Health Systems Tutorial Slides S2 2025
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Business Ethics Teaching Materials for college
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Mga Unang Hakbang Tungo Sa Tao by Joe Vibar Nero.pdf
How to Manage Starshipit in Odoo 18 - Odoo Slides
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Piense y hagase Rico - Napoleon Hill Ccesa007.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
English Language Teaching from Post-.pdf
UPPER GASTRO INTESTINAL DISORDER.docx
The Final Stretch: How to Release a Game and Not Die in the Process.
STATICS OF THE RIGID BODIES Hibbelers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
Cell Structure & Organelles in detailed.

Event Handling in java

  • 2. Some common term used in EventHandlingEvent : 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 HandlingThere 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 ClassAction 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 Listenerpublic 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); } }}
  • 13. Key Event ClassThis class has 3 methods in Listener interfaceNamely: 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 themjf.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 ClassThis 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 ClassThis 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 themjf.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 ClassThis 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. Examplepackage 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 ClassThis 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 ClassThis 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….