SlideShare una empresa de Scribd logo
1 de 6
Java Event Handling
In AWT components, we came to know every
component (except Panel and Label) generates
events when interacted by the user like clicking over
a button or pressing enter key etc. Listeners handle
the events. Let us know the style (or design pattern)
Java follows to handle the events.

The event handling involves three types of classes.
  1. Event Sources
  2. Event classes
  3. Event Listeners
1. Event Sources
Event sources are components, subclasses of
java.awt.Component, capable to generate events.
The event source can be a button, TextField or a
Frame etc.
java.awt.Component
After having the basic idea of Java AWT, let us know
how many component classes exist with java.awt
package.
2. Event classes
Almost every event source generates an event and is
named by some Java class. For example, the button
generates ActionEvent and Checkbox generates
ItemEvent. All events listed in java.awt.event
package. Following list gives a few components and
their listeners.
3. Event Listeners
The events generated by the GUI components are
handled by a special group of classes known as
"listeners". Listener is an interface. Every
component       has   its   own     listener, say,
AdjustmentListener handles the events of scrollbar
Some listeners handle the events of a few
components. For example, ActionListener handles
the events of Button, TextField, List and Menus.
Listeners are from java.awt.event package.
Handle Action Events for AWT Button Example
import java.applet.Applet;
import java.awt.Button;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/*
<applet        code="HandleActionEventExample"
width=200 height=200>
</applet>
*/

public class HandleActionEventExample extends
Applet implements ActionListener
{

   String actionMessage="";

    public void init()
   {
       //create Buttons
       Button Button1 = new Button("Ok");
       Button Button2 = new Button("Cancel");

       //add Buttons
       add(Button1);
       add(Button2);

       //set action listeners for buttons
       Button1.addActionListener(this);
       Button2.addActionListener(this);
   }

   public void paint(Graphics g)
   {
      g.drawString(actionMessage,10,50);
}


public void actionPerformed(ActionEvent ae)
   {

          String action = ae.getActionCommand();

       if(action.equals("Ok"))
            actionMessage = "Ok Button Pressed";
       else if(action.equals("Cancel"))
                    actionMessage = "Cancel Button
Pressed";

          repaint();
      }
  }

Más contenido relacionado

La actualidad más candente

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
 
JAVA GUI PART III
JAVA GUI PART IIIJAVA GUI PART III
JAVA GUI PART IIIOXUS 20
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in JavaAyesha Kanwal
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and ComponentsSohanur63
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpointRicardo Garcia
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03Ankit Dubey
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solutionMazenetsolution
 
Java gui event
Java gui eventJava gui event
Java gui eventSoftNutx
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to buttonyugandhar vadlamudi
 

La actualidad más candente (19)

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
 
JAVA GUI PART III
JAVA GUI PART IIIJAVA GUI PART III
JAVA GUI PART III
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
25 awt
25 awt25 awt
25 awt
 
AWT Packages , Containers and Components
AWT Packages , Containers and ComponentsAWT Packages , Containers and Components
AWT Packages , Containers and Components
 
Java awt tutorial javatpoint
Java awt tutorial   javatpointJava awt tutorial   javatpoint
Java awt tutorial javatpoint
 
Swing
SwingSwing
Swing
 
What is Event
What is EventWhat is Event
What is Event
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
Java- GUI- Mazenet solution
Java- GUI- Mazenet solutionJava- GUI- Mazenet solution
Java- GUI- Mazenet solution
 
Java: GUI
Java: GUIJava: GUI
Java: GUI
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Unit 6 Java
Unit 6 JavaUnit 6 Java
Unit 6 Java
 
GUI components in Java
GUI components in JavaGUI components in Java
GUI components in Java
 
28 awt
28 awt28 awt
28 awt
 
Awt
AwtAwt
Awt
 
Adding a action listener to button
Adding a action listener to buttonAdding a action listener to button
Adding a action listener to button
 
AWT
AWT AWT
AWT
 

Destacado

31policies
31policies31policies
31policiesmuthitae
 
Multimedia learning-principles
Multimedia learning-principlesMultimedia learning-principles
Multimedia learning-principlesWind Vento
 
Your future is secured
Your future is securedYour future is secured
Your future is securedslanw
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqPradeep Bisht
 
Sap 13-msdm-09-10
Sap 13-msdm-09-10Sap 13-msdm-09-10
Sap 13-msdm-09-104545wr
 
Etapas del desarrollo historico de la quimica
Etapas del desarrollo historico de la quimicaEtapas del desarrollo historico de la quimica
Etapas del desarrollo historico de la quimicadaesel
 

Destacado (8)

31policies
31policies31policies
31policies
 
Multimedia learning-principles
Multimedia learning-principlesMultimedia learning-principles
Multimedia learning-principles
 
Your future is secured
Your future is securedYour future is secured
Your future is secured
 
pradeepbishtLecture13 div conq
pradeepbishtLecture13 div conqpradeepbishtLecture13 div conq
pradeepbishtLecture13 div conq
 
Social Media Guide
Social Media GuideSocial Media Guide
Social Media Guide
 
Floyd aaaaaa
Floyd aaaaaaFloyd aaaaaa
Floyd aaaaaa
 
Sap 13-msdm-09-10
Sap 13-msdm-09-10Sap 13-msdm-09-10
Sap 13-msdm-09-10
 
Etapas del desarrollo historico de la quimica
Etapas del desarrollo historico de la quimicaEtapas del desarrollo historico de la quimica
Etapas del desarrollo historico de la quimica
 

Similar a Lecture8 oopj

Event handling
Event handlingEvent handling
Event handlingswapnac12
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptsharanyak0721
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Javasuraj pandey
 
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
 
ACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptxACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptxMattFlordeliza1
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptxGood657694
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.pptusama537223
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVASrajan Shukla
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5sotlsoc
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 appletsraksharao
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2PRN USM
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxudithaisur
 

Similar a Lecture8 oopj (20)

Event handling
Event handlingEvent handling
Event handling
 
engineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.pptengineeringdsgtnotesofunitfivesnists.ppt
engineeringdsgtnotesofunitfivesnists.ppt
 
Basic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in JavaBasic of Abstract Window Toolkit(AWT) in Java
Basic of Abstract Window Toolkit(AWT) in Java
 
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
 
JAVA AWT
JAVA AWTJAVA AWT
JAVA AWT
 
ACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptxACtionlistener in java use in discussion.pptx
ACtionlistener in java use in discussion.pptx
 
09events
09events09events
09events
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
Md11 gui event handling
Md11 gui event handlingMd11 gui event handling
Md11 gui event handling
 
CORE JAVA-2
CORE JAVA-2CORE JAVA-2
CORE JAVA-2
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
Java
JavaJava
Java
 
Chapter 11.5
Chapter 11.5Chapter 11.5
Chapter 11.5
 
java Unit4 chapter1 applets
java Unit4 chapter1 appletsjava Unit4 chapter1 applets
java Unit4 chapter1 applets
 
Gu iintro(java)
Gu iintro(java)Gu iintro(java)
Gu iintro(java)
 
Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2Graphical User Interface (GUI) - 2
Graphical User Interface (GUI) - 2
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 
Lecture9 oopj
Lecture9 oopjLecture9 oopj
Lecture9 oopj
 

Lecture8 oopj

  • 1. Java Event Handling In AWT components, we came to know every component (except Panel and Label) generates events when interacted by the user like clicking over a button or pressing enter key etc. Listeners handle the events. Let us know the style (or design pattern) Java follows to handle the events. The event handling involves three types of classes. 1. Event Sources 2. Event classes 3. Event Listeners 1. Event Sources Event sources are components, subclasses of java.awt.Component, capable to generate events. The event source can be a button, TextField or a Frame etc. java.awt.Component After having the basic idea of Java AWT, let us know how many component classes exist with java.awt package.
  • 2. 2. Event classes Almost every event source generates an event and is named by some Java class. For example, the button generates ActionEvent and Checkbox generates ItemEvent. All events listed in java.awt.event package. Following list gives a few components and their listeners.
  • 3. 3. Event Listeners The events generated by the GUI components are handled by a special group of classes known as "listeners". Listener is an interface. Every component has its own listener, say, AdjustmentListener handles the events of scrollbar Some listeners handle the events of a few components. For example, ActionListener handles the events of Button, TextField, List and Menus. Listeners are from java.awt.event package.
  • 4. Handle Action Events for AWT Button Example import java.applet.Applet; import java.awt.Button; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; /*
  • 5. <applet code="HandleActionEventExample" width=200 height=200> </applet> */ public class HandleActionEventExample extends Applet implements ActionListener { String actionMessage=""; public void init() { //create Buttons Button Button1 = new Button("Ok"); Button Button2 = new Button("Cancel"); //add Buttons add(Button1); add(Button2); //set action listeners for buttons Button1.addActionListener(this); Button2.addActionListener(this); } public void paint(Graphics g) { g.drawString(actionMessage,10,50);
  • 6. } public void actionPerformed(ActionEvent ae) { String action = ae.getActionCommand(); if(action.equals("Ok")) actionMessage = "Ok Button Pressed"; else if(action.equals("Cancel")) actionMessage = "Cancel Button Pressed"; repaint(); } }