SlideShare a Scribd company logo
1 of 16
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

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
 
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
 
Unit-3 event handling
Unit-3 event handlingUnit-3 event handling
Unit-3 event handling
 

Recently uploaded

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130Suhani Kapoor
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Serviceranjana rawat
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations120cr0395
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxJoão Esperancinha
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxwendy cai
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVRajaP95
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxupamatechverse
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escortsranjana rawat
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝soniya singh
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Dr.Costas Sachpazis
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingrakeshbaidya232001
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...Soham Mondal
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxhumanexperienceaaa
 

Recently uploaded (20)

VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
VIP Call Girls Service Kondapur Hyderabad Call +91-8250192130
 
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
(RIA) Call Girls Bhosari ( 7001035870 ) HI-Fi Pune Escorts Service
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Extrusion Processes and Their Limitations
Extrusion Processes and Their LimitationsExtrusion Processes and Their Limitations
Extrusion Processes and Their Limitations
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptxDecoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
Decoding Kotlin - Your guide to solving the mysterious in Kotlin.pptx
 
What are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptxWhat are the advantages and disadvantages of membrane structures.pptx
What are the advantages and disadvantages of membrane structures.pptx
 
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IVHARMONY IN THE NATURE AND EXISTENCE - Unit-IV
HARMONY IN THE NATURE AND EXISTENCE - Unit-IV
 
Introduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptxIntroduction to IEEE STANDARDS and its different types.pptx
Introduction to IEEE STANDARDS and its different types.pptx
 
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
(MEERA) Dapodi Call Girls Just Call 7001035870 [ Cash on Delivery ] Pune Escorts
 
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
Call Girls in Nagpur Suman Call 7001035870 Meet With Nagpur Escorts
 
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
Model Call Girl in Narela Delhi reach out to us at 🔝8264348440🔝
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANJALI) Dange Chowk Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
Structural Analysis and Design of Foundations: A Comprehensive Handbook for S...
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 
Porous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writingPorous Ceramics seminar and technical writing
Porous Ceramics seminar and technical writing
 
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
OSVC_Meta-Data based Simulation Automation to overcome Verification Challenge...
 
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINEDJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
DJARUM4D - SLOT GACOR ONLINE | SLOT DEMO ONLINE
 
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptxthe ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
the ladakh protest in leh ladakh 2024 sonam wangchuk.pptx
 

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); }}