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 (20)

Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
Ā 
Java awt
Java awtJava awt
Java awt
Ā 
Java I/o streams
Java I/o streamsJava I/o streams
Java I/o streams
Ā 
Event handling
Event handlingEvent handling
Event handling
Ā 
Awt controls ppt
Awt controls pptAwt controls ppt
Awt controls ppt
Ā 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Ā 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
Ā 
Interface in java
Interface in javaInterface in java
Interface in java
Ā 
Presentation on-exception-handling
Presentation on-exception-handlingPresentation on-exception-handling
Presentation on-exception-handling
Ā 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
Ā 
Swing and AWT in java
Swing and AWT in javaSwing and AWT in java
Swing and AWT in java
Ā 
Jsp ppt
Jsp pptJsp ppt
Jsp ppt
Ā 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
Ā 
Method overloading
Method overloadingMethod overloading
Method overloading
Ā 
Ado.Net Tutorial
Ado.Net TutorialAdo.Net Tutorial
Ado.Net Tutorial
Ā 
Java threads
Java threadsJava threads
Java threads
Ā 
Java: GUI
Java: GUIJava: GUI
Java: GUI
Ā 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
Ā 
Asp.net state management
Asp.net state managementAsp.net state management
Asp.net state management
Ā 
Java Streams
Java StreamsJava Streams
Java Streams
Ā 

Viewers also liked

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, 3jandrewsxu
Ā 
Bluetooth technology
Bluetooth technologyBluetooth technology
Bluetooth technologyRohit Roy
Ā 
software project management Waterfall model
software project management Waterfall modelsoftware project management Waterfall model
software project management Waterfall modelREHMAT ULLAH
Ā 
Classical problem of synchronization
Classical problem of synchronizationClassical problem of synchronization
Classical problem of synchronizationShakshi Ranawat
Ā 
Visibility control in java
Visibility control in javaVisibility control in java
Visibility control in javaTech_MX
Ā 
Wi-Fi vs Bluetooth
Wi-Fi vs BluetoothWi-Fi vs Bluetooth
Wi-Fi vs BluetoothArun ACE
Ā 
Java access modifiers
Java access modifiersJava access modifiers
Java access modifiersSrinivas Reddy
Ā 
Waterfall model ppt final
Waterfall model ppt  finalWaterfall model ppt  final
Waterfall model ppt finalshiva krishna
Ā 
Wi-Fi Technology
Wi-Fi TechnologyWi-Fi Technology
Wi-Fi TechnologyNaveen Kumar
Ā 

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) - 2PRN USM
Ā 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
Ā 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.pptusama537223
Ā 
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. 2024kashyapneha2809
Ā 
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. 2024nehakumari0xf
Ā 
Advance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingAdvance Java Programming(CM5I) Event handling
Advance Java Programming(CM5I) Event handlingPayal Dungarwal
Ā 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handlingAmol Gaikwad
Ā 
tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handlingteach4uin
Ā 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
Ā 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
Ā 
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 programmingSynapseindiappsdevelopment
Ā 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
Ā 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxusvirat1805
Ā 
Event handling in Java(part 2)
Event handling in Java(part 2)Event handling in Java(part 2)
Event handling in Java(part 2)RAJITHARAMACHANDRAN1
Ā 

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
Ā 
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
Ā 
Event handling in Java(part 2)
Event handling in Java(part 2)Event handling in Java(part 2)
Event handling in Java(part 2)
Ā 

Recently uploaded

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Jisc
Ā 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structuredhanjurrannsibayan2
Ā 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
Ā 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxheathfieldcps1
Ā 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
Ā 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
Ā 
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).pptxVishalSingh1417
Ā 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
Ā 
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...christianmathematics
Ā 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
Ā 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
Ā 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentationcamerronhm
Ā 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
Ā 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
Ā 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
Ā 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptxMaritesTamaniVerdade
Ā 
Tį»”NG ƔN Tįŗ¬P THI VƀO Lį»šP 10 MƔN TIįŗ¾NG ANH NĂM Hį»ŒC 2023 - 2024 CƓ ĐƁP ƁN (NGį»® Ƃ...
Tį»”NG ƔN Tįŗ¬P THI VƀO Lį»šP 10 MƔN TIįŗ¾NG ANH NĂM Hį»ŒC 2023 - 2024 CƓ ĐƁP ƁN (NGį»® Ƃ...Tį»”NG ƔN Tįŗ¬P THI VƀO Lį»šP 10 MƔN TIįŗ¾NG ANH NĂM Hį»ŒC 2023 - 2024 CƓ ĐƁP ƁN (NGį»® Ƃ...
Tį»”NG ƔN Tįŗ¬P THI VƀO Lį»šP 10 MƔN TIįŗ¾NG ANH NĂM Hį»ŒC 2023 - 2024 CƓ ĐƁP ƁN (NGį»® Ƃ...Nguyen Thanh Tu Collection
Ā 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibitjbellavia9
Ā 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
Ā 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
Ā 

Recently uploaded (20)

Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)Accessible Digital Futures project (20/03/2024)
Accessible Digital Futures project (20/03/2024)
Ā 
Single or Multiple melodic lines structure
Single or Multiple melodic lines structureSingle or Multiple melodic lines structure
Single or Multiple melodic lines structure
Ā 
Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
Ā 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
Ā 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
Ā 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
Ā 
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
Ā 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
Ā 
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...
Ā 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
Ā 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
Ā 
SOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning PresentationSOC 101 Demonstration of Learning Presentation
SOC 101 Demonstration of Learning Presentation
Ā 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Ā 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
Ā 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
Ā 
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
2024-NATIONAL-LEARNING-CAMP-AND-OTHER.pptx
Ā 
Tį»”NG ƔN Tįŗ¬P THI VƀO Lį»šP 10 MƔN TIįŗ¾NG ANH NĂM Hį»ŒC 2023 - 2024 CƓ ĐƁP ƁN (NGį»® Ƃ...
Tį»”NG ƔN Tįŗ¬P THI VƀO Lį»šP 10 MƔN TIįŗ¾NG ANH NĂM Hį»ŒC 2023 - 2024 CƓ ĐƁP ƁN (NGį»® Ƃ...Tį»”NG ƔN Tįŗ¬P THI VƀO Lį»šP 10 MƔN TIįŗ¾NG ANH NĂM Hį»ŒC 2023 - 2024 CƓ ĐƁP ƁN (NGį»® Ƃ...
Tį»”NG ƔN Tįŗ¬P THI VƀO Lį»šP 10 MƔN TIįŗ¾NG ANH NĂM Hį»ŒC 2023 - 2024 CƓ ĐƁP ƁN (NGį»® Ƃ...
Ā 
Sociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning ExhibitSociology 101 Demonstration of Learning Exhibit
Sociology 101 Demonstration of Learning Exhibit
Ā 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
Ā 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
Ā 

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ā€ā€¦.
  • 19. Output after releasing ā€œ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ā€¦.