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)

Interface
InterfaceInterface
Interface
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
Event handling
Event handlingEvent handling
Event handling
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
 
JDBC: java DataBase connectivity
JDBC: java DataBase connectivityJDBC: java DataBase connectivity
JDBC: java DataBase connectivity
 
Java threads
Java threadsJava threads
Java threads
 
Life cycle-of-a-thread
Life cycle-of-a-threadLife cycle-of-a-thread
Life cycle-of-a-thread
 
Java awt (abstract window toolkit)
Java awt (abstract window toolkit)Java awt (abstract window toolkit)
Java awt (abstract window toolkit)
 
input/ output in java
input/ output  in javainput/ output  in java
input/ output in java
 
JVM
JVMJVM
JVM
 
Inheritance in java
Inheritance in javaInheritance in java
Inheritance in java
 
Java Collections
Java  Collections Java  Collections
Java Collections
 
JDBC – Java Database Connectivity
JDBC – Java Database ConnectivityJDBC – Java Database Connectivity
JDBC – Java Database Connectivity
 
Java awt
Java awtJava awt
Java awt
 
String and string buffer
String and string bufferString and string buffer
String and string buffer
 
Java interfaces
Java interfacesJava interfaces
Java interfaces
 
9. Input Output in java
9. Input Output in java9. Input Output in java
9. Input Output in java
 
Packages in java
Packages in javaPackages in java
Packages in java
 
Exception Handling in JAVA
Exception Handling in JAVAException Handling in JAVA
Exception Handling in JAVA
 

Viewers also liked

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. 2024nehakumari0xf
 
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
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
 
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
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in JavaAyesha Kanwal
 
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
 

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
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
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
 

Recently uploaded

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxpboyjonauth
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docxPoojaSen20
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application ) Sakshi Ghasle
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsKarinaGenton
 
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
 
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
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxNirmalaLoungPoorunde1
 
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
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...EduSkills OECD
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17Celine George
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docxPoojaSen20
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentInMediaRes1
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 

Recently uploaded (20)

Introduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptxIntroduction to AI in Higher Education_draft.pptx
Introduction to AI in Higher Education_draft.pptx
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
MENTAL STATUS EXAMINATION format.docx
MENTAL     STATUS EXAMINATION format.docxMENTAL     STATUS EXAMINATION format.docx
MENTAL STATUS EXAMINATION format.docx
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Hybridoma Technology ( Production , Purification , and Application )
Hybridoma Technology  ( Production , Purification , and Application  ) Hybridoma Technology  ( Production , Purification , and Application  )
Hybridoma Technology ( Production , Purification , and Application )
 
9953330565 Low Rate Call Girls In Rohini Delhi NCR
9953330565 Low Rate Call Girls In Rohini  Delhi NCR9953330565 Low Rate Call Girls In Rohini  Delhi NCR
9953330565 Low Rate Call Girls In Rohini Delhi NCR
 
Science 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its CharacteristicsScience 7 - LAND and SEA BREEZE and its Characteristics
Science 7 - LAND and SEA BREEZE and its Characteristics
 
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
 
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
 
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
 
Employee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptxEmployee wellbeing at the workplace.pptx
Employee wellbeing at the workplace.pptx
 
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...
 
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
Presentation by Andreas Schleicher Tackling the School Absenteeism Crisis 30 ...
 
How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17How to Configure Email Server in Odoo 17
How to Configure Email Server in Odoo 17
 
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
 
mini mental status format.docx
mini    mental       status     format.docxmini    mental       status     format.docx
mini mental status format.docx
 
Alper Gobel In Media Res Media Component
Alper Gobel In Media Res Media ComponentAlper Gobel In Media Res Media Component
Alper Gobel In Media Res Media Component
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
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
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 

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….