SlideShare a Scribd company logo
Pundra University Of Science
& Technology
WELCOME
File Handling
Events and Listeners
In event-driven programming, code is executed upon
activation of events
An event can be defined as a type of signal to the program that
something has happened
The event is generated by external user actions such as:
Moving the mouse
Clicking the button
Pressing a key
Sliding the scrollbar
Choosing an item from a menu
Events are responded to by event listeners
Event Handling Model
• To process an event
– Register an event listener
– Implement event handler
• Method that is called in response to an event
• Each event handling interface has one or more event
handling methods that must be defined
The Event Handling process
When an event is triggered, the JAVA runtime first
determines its source and type
If a listener for this type of event is registered with the
source, an event object is created
For each listener to this type of an event
The JAVA runtime invokes the appropriate event handling
method to the listener and passes the event object as the
parameter
Selected User Actions
User Action
Click a button
Select a new item
Select an item from a List
Window opened, closed
Mouse pressed, released
Key released, pressed
Event Type Generated
ActionEvent
ItemEvent, ActionEvent
ListSelectionEvent
WindowEvent
MouseEvent
KeyEvent
Source Object
JButton
JComboBox
JList
Window
MouseEvent
KeyEvent
Java AWT Event Listener Interfaces
 ActionListener
 ItemListener
 KeyListener
 MouseListener
 WindowListener
 ListSelectionListener
Event Listener and Action Listener
• An Event Listener, once set to an applet object waits for some
action to be performed on it
– It mouse click, mouse hover, pressing of keys, click of
button, etc
• ActionListener is an interface that could be implemented in
order to determine how certain event should be handled
• When implementing an interface, all methods in that interface
should be implemented, ActionListener interface has one
method to implement named actionPerformed()
How to Implement a Listener Interface
Use the implements keyword in the class declaration
Register the object as a listener for a component’s event, using
the component’s addXListener method. (where X is the type of
event).
Declare and fully define all methods for the interface that you
are implementing
Three Steps of Event Handling
Prepare to accept events
• import package java.awt.event
Start listening for events
• include appropriate methods
Respond to events
• implement appropriate abstract method
Prepare to accept events
• Import package java.awt.event
• Applet manifests its desire to accept events by promising to
“implement” certain methods
• Example:
– “ActionListener” for Button events
– “AdjustmentListener” for Scrollbar events
Start listening for events
• To make the applet “listen” to a particular event, include the
appropriate “addxxxListener”.
• Examples:
addActionListener(this)
– shows that the applet is interested in listening to events
generated by the pushing of a certain button
addAdjustmentListener(this)
– shows that the applet is interested in listening to events
generated by the sliding of a certain scroll bar
Respond to events
• The appropriate abstract methods are implemented.
• Example:
– actionPerformed() is automatically called whenever the user
clicks the button
• Thus, implement actionPerformed() to respond to the button event
– adjustmentValueChanged() is automatically invoked whenever
the user slides the scroll bar thumb
• So adjustmentValueChanged() needs to be implemented
• In actionPerformed(ActionEvent evt), ActionEvent is
a class in java.awt.event
ActionEvent
In Java, most components have a special event called an
ActionEvent
This is loosely speaking the most common or canonical
event for that component
A good example is a click for a button
To have any component listen for ActionEvents, you must
register the component with an ActionListener
– e.g. button.addActionListener(new MyAL());
ActionPerformed
The actionPerformed method has the following signature:
» void actionPerformed(ActionEvent)
The object of type ActionEvent passed to the event
handler is used to query information about the event
Some common methods are:
getSource()
object reference to component generating event
getActionCommand()
some text associated with event (text on button, etc)
Event Handler Code
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class Test2 extends JFrame implements
ActionListener
{
JFrame myFrame;
JTextField myTextField1;
JButton myButton1;
JLabel myLabel;
JLabel myLabel1;
public void simple()
{
myFrame=new JFrame("My Frame");
myFrame.setLayout(new FlowLayout());
myLabel=new JLabel("Simple GUI Frame");
myLabel1=new JLabel("Enter Input");
myTextField1=new JTextField(10);
myButton1=new JButton("Clear");
myFrame.setLayout(new GridLayout(15,1));
myFrame.add(myLabel);
myFrame.add(myLabel1);
myFrame.add(myTextField1);
myFrame.add(myButton1);
myFrame.setSize(400,400);
myFrame.setVisible(true);
myButton1.addActionListener(this);
}
public void actionPerformed(ActionEvent evt)
{
JButton source=(JButton)evt.getSource();
if(source == myButton1)
myTextField1.setText("");
}
public static void main(String[] args)
{
Test2 test = new Test2();
test.simple();
test.setDefaultCloseOperation(JFrame.EXIT_ON_CL
OSE);
}}

More Related Content

Similar to File Handling

tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
teach4uin
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
usvirat1805
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
Ayesha Kanwal
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Jyothishmathi Institute of Technology and Science Karimnagar
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
Ankit Dubey
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
Srajan Shukla
 
Events1
Events1Events1
Events1
Nuha Noor
 
What is Event
What is EventWhat is Event
What is Event
Asmita Prasad
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
simmis5
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
usama537223
 
Java gui event
Java gui eventJava gui event
Java gui event
SoftNutx
 
Event handling
Event handlingEvent handling
Event handling
Ravi_Kant_Sahu
 
Event handling
Event handlingEvent handling
Event handling
Ravi Kant Sahu
 
Event handling
Event handlingEvent handling
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptx
Tanzila Kehkashan
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
TadeseBeyene
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part II
SivaSankari36
 
Asp.net event handler
Asp.net event handlerAsp.net event handler
Asp.net event handler
Sireesh K
 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)
RAJITHARAMACHANDRAN1
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
udithaisur
 

Similar to File Handling (20)

tL20 event handling
tL20 event handlingtL20 event handling
tL20 event handling
 
event-handling.pptx
event-handling.pptxevent-handling.pptx
event-handling.pptx
 
Event Handling in Java
Event Handling in JavaEvent Handling in Java
Event Handling in Java
 
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing ButtonsJAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
 
Ajp notes-chapter-03
Ajp notes-chapter-03Ajp notes-chapter-03
Ajp notes-chapter-03
 
Event Handling in JAVA
Event Handling in JAVAEvent Handling in JAVA
Event Handling in JAVA
 
Events1
Events1Events1
Events1
 
What is Event
What is EventWhat is Event
What is Event
 
Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)Java Programming :Event Handling(Types of Events)
Java Programming :Event Handling(Types of Events)
 
event handling new.ppt
event handling new.pptevent handling new.ppt
event handling new.ppt
 
Java gui event
Java gui eventJava gui event
Java gui event
 
Event handling
Event handlingEvent handling
Event handling
 
Event handling
Event handlingEvent handling
Event handling
 
Event handling
Event handlingEvent handling
Event handling
 
OOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptxOOP Lecture 11-EventHandling1.pptx
OOP Lecture 11-EventHandling1.pptx
 
Chap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptxChap - 2 - Event Handling.pptx
Chap - 2 - Event Handling.pptx
 
PROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part IIPROGRAMMING IN JAVA- unit 4-part II
PROGRAMMING IN JAVA- unit 4-part II
 
Asp.net event handler
Asp.net event handlerAsp.net event handler
Asp.net event handler
 
Event handling in Java(part 1)
Event handling in Java(part 1)Event handling in Java(part 1)
Event handling in Java(part 1)
 
ITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptxITE 1122_ Event Handling.pptx
ITE 1122_ Event Handling.pptx
 

Recently uploaded

Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Christina Lin
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
kandramariana6
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
ssuser36d3051
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
Aditya Rajan Patra
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
rpskprasana
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
awadeshbabu
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
Divyam548318
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
insn4465
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
nooriasukmaningtyas
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
drwaing
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
IJNSA Journal
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
jpsjournal1
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
IJECEIAES
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
PuktoonEngr
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
JamalHussainArman
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
RadiNasr
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
mamunhossenbd75
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
gerogepatton
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
IJECEIAES
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
gestioneergodomus
 

Recently uploaded (20)

Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming PipelinesHarnessing WebAssembly for Real-time Stateless Streaming Pipelines
Harnessing WebAssembly for Real-time Stateless Streaming Pipelines
 
132/33KV substation case study Presentation
132/33KV substation case study Presentation132/33KV substation case study Presentation
132/33KV substation case study Presentation
 
sieving analysis and results interpretation
sieving analysis and results interpretationsieving analysis and results interpretation
sieving analysis and results interpretation
 
Recycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part IIIRecycled Concrete Aggregate in Construction Part III
Recycled Concrete Aggregate in Construction Part III
 
CSM Cloud Service Management Presentarion
CSM Cloud Service Management PresentarionCSM Cloud Service Management Presentarion
CSM Cloud Service Management Presentarion
 
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
[JPP-1] - (JEE 3.0) - Kinematics 1D - 14th May..pdf
 
bank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdfbank management system in java and mysql report1.pdf
bank management system in java and mysql report1.pdf
 
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
哪里办理(csu毕业证书)查尔斯特大学毕业证硕士学历原版一模一样
 
A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...A review on techniques and modelling methodologies used for checking electrom...
A review on techniques and modelling methodologies used for checking electrom...
 
digital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdfdigital fundamental by Thomas L.floydl.pdf
digital fundamental by Thomas L.floydl.pdf
 
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMSA SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
A SYSTEMATIC RISK ASSESSMENT APPROACH FOR SECURING THE SMART IRRIGATION SYSTEMS
 
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECTCHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
CHINA’S GEO-ECONOMIC OUTREACH IN CENTRAL ASIAN COUNTRIES AND FUTURE PROSPECT
 
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
Electric vehicle and photovoltaic advanced roles in enhancing the financial p...
 
2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt2. Operations Strategy in a Global Environment.ppt
2. Operations Strategy in a Global Environment.ppt
 
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptxML Based Model for NIDS MSc Updated Presentation.v2.pptx
ML Based Model for NIDS MSc Updated Presentation.v2.pptx
 
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdfIron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
Iron and Steel Technology Roadmap - Towards more sustainable steelmaking.pdf
 
Heat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation pptHeat Resistant Concrete Presentation ppt
Heat Resistant Concrete Presentation ppt
 
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODELDEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
DEEP LEARNING FOR SMART GRID INTRUSION DETECTION: A HYBRID CNN-LSTM-BASED MODEL
 
Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...Advanced control scheme of doubly fed induction generator for wind turbine us...
Advanced control scheme of doubly fed induction generator for wind turbine us...
 
DfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributionsDfMAy 2024 - key insights and contributions
DfMAy 2024 - key insights and contributions
 

File Handling

  • 1. Pundra University Of Science & Technology WELCOME
  • 3. Events and Listeners In event-driven programming, code is executed upon activation of events An event can be defined as a type of signal to the program that something has happened The event is generated by external user actions such as: Moving the mouse Clicking the button Pressing a key Sliding the scrollbar Choosing an item from a menu Events are responded to by event listeners
  • 4. Event Handling Model • To process an event – Register an event listener – Implement event handler • Method that is called in response to an event • Each event handling interface has one or more event handling methods that must be defined
  • 5. The Event Handling process When an event is triggered, the JAVA runtime first determines its source and type If a listener for this type of event is registered with the source, an event object is created For each listener to this type of an event The JAVA runtime invokes the appropriate event handling method to the listener and passes the event object as the parameter
  • 6. Selected User Actions User Action Click a button Select a new item Select an item from a List Window opened, closed Mouse pressed, released Key released, pressed Event Type Generated ActionEvent ItemEvent, ActionEvent ListSelectionEvent WindowEvent MouseEvent KeyEvent Source Object JButton JComboBox JList Window MouseEvent KeyEvent
  • 7. Java AWT Event Listener Interfaces  ActionListener  ItemListener  KeyListener  MouseListener  WindowListener  ListSelectionListener
  • 8. Event Listener and Action Listener • An Event Listener, once set to an applet object waits for some action to be performed on it – It mouse click, mouse hover, pressing of keys, click of button, etc • ActionListener is an interface that could be implemented in order to determine how certain event should be handled • When implementing an interface, all methods in that interface should be implemented, ActionListener interface has one method to implement named actionPerformed()
  • 9. How to Implement a Listener Interface Use the implements keyword in the class declaration Register the object as a listener for a component’s event, using the component’s addXListener method. (where X is the type of event). Declare and fully define all methods for the interface that you are implementing
  • 10. Three Steps of Event Handling Prepare to accept events • import package java.awt.event Start listening for events • include appropriate methods Respond to events • implement appropriate abstract method
  • 11. Prepare to accept events • Import package java.awt.event • Applet manifests its desire to accept events by promising to “implement” certain methods • Example: – “ActionListener” for Button events – “AdjustmentListener” for Scrollbar events
  • 12. Start listening for events • To make the applet “listen” to a particular event, include the appropriate “addxxxListener”. • Examples: addActionListener(this) – shows that the applet is interested in listening to events generated by the pushing of a certain button addAdjustmentListener(this) – shows that the applet is interested in listening to events generated by the sliding of a certain scroll bar
  • 13. Respond to events • The appropriate abstract methods are implemented. • Example: – actionPerformed() is automatically called whenever the user clicks the button • Thus, implement actionPerformed() to respond to the button event – adjustmentValueChanged() is automatically invoked whenever the user slides the scroll bar thumb • So adjustmentValueChanged() needs to be implemented • In actionPerformed(ActionEvent evt), ActionEvent is a class in java.awt.event
  • 14. ActionEvent In Java, most components have a special event called an ActionEvent This is loosely speaking the most common or canonical event for that component A good example is a click for a button To have any component listen for ActionEvents, you must register the component with an ActionListener – e.g. button.addActionListener(new MyAL());
  • 15. ActionPerformed The actionPerformed method has the following signature: » void actionPerformed(ActionEvent) The object of type ActionEvent passed to the event handler is used to query information about the event Some common methods are: getSource() object reference to component generating event getActionCommand() some text associated with event (text on button, etc)
  • 16. Event Handler Code import java.awt.*; import java.awt.event.*; import javax.swing.*; public class Test2 extends JFrame implements ActionListener { JFrame myFrame; JTextField myTextField1; JButton myButton1; JLabel myLabel; JLabel myLabel1; public void simple() { myFrame=new JFrame("My Frame"); myFrame.setLayout(new FlowLayout()); myLabel=new JLabel("Simple GUI Frame"); myLabel1=new JLabel("Enter Input"); myTextField1=new JTextField(10); myButton1=new JButton("Clear"); myFrame.setLayout(new GridLayout(15,1)); myFrame.add(myLabel); myFrame.add(myLabel1); myFrame.add(myTextField1); myFrame.add(myButton1); myFrame.setSize(400,400); myFrame.setVisible(true); myButton1.addActionListener(this); } public void actionPerformed(ActionEvent evt) { JButton source=(JButton)evt.getSource(); if(source == myButton1) myTextField1.setText(""); } public static void main(String[] args) { Test2 test = new Test2(); test.simple(); test.setDefaultCloseOperation(JFrame.EXIT_ON_CL OSE); }}