SlideShare ist ein Scribd-Unternehmen logo
1 von 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);
}}

Weitere ähnliche Inhalte

Ähnlich wie File Handling

Ähnlich wie 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
 

Kürzlich hochgeladen

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapRishantSharmaFr
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756dollysharma2066
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfJiananWang21
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startQuintin Balsdon
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxJuliansyahHarahap1
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoordharasingh5698
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...roncy bisnoi
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086anil_gaur
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTbhaskargani46
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptxJIT KUMAR GUPTA
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptNANDHAKUMARA10
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfKamal Acharya
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . pptDineshKumar4165
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...SUHANI PANDEY
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptDineshKumar4165
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...tanu pandey
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Arindam Chakraborty, Ph.D., P.E. (CA, TX)
 

Kürzlich hochgeladen (20)

Unleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leapUnleashing the Power of the SORA AI lastest leap
Unleashing the Power of the SORA AI lastest leap
 
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
FULL ENJOY Call Girls In Mahipalpur Delhi Contact Us 8377877756
 
data_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdfdata_management_and _data_science_cheat_sheet.pdf
data_management_and _data_science_cheat_sheet.pdf
 
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Netaji Nagar, Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
Design For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the startDesign For Accessibility: Getting it right from the start
Design For Accessibility: Getting it right from the start
 
Work-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptxWork-Permit-Receiver-in-Saudi-Aramco.pptx
Work-Permit-Receiver-in-Saudi-Aramco.pptx
 
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoorTop Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
Top Rated Call Girls In chittoor 📱 {7001035870} VIP Escorts chittoor
 
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
Call Girls Pimpri Chinchwad Call Me 7737669865 Budget Friendly No Advance Boo...
 
Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086Minimum and Maximum Modes of microprocessor 8086
Minimum and Maximum Modes of microprocessor 8086
 
Generative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPTGenerative AI or GenAI technology based PPT
Generative AI or GenAI technology based PPT
 
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
COST-EFFETIVE  and Energy Efficient BUILDINGS ptxCOST-EFFETIVE  and Energy Efficient BUILDINGS ptx
COST-EFFETIVE and Energy Efficient BUILDINGS ptx
 
Block diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.pptBlock diagram reduction techniques in control systems.ppt
Block diagram reduction techniques in control systems.ppt
 
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdfONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
ONLINE FOOD ORDER SYSTEM PROJECT REPORT.pdf
 
Thermal Engineering Unit - I & II . ppt
Thermal Engineering  Unit - I & II . pptThermal Engineering  Unit - I & II . ppt
Thermal Engineering Unit - I & II . ppt
 
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort ServiceCall Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
Call Girls in Ramesh Nagar Delhi 💯 Call Us 🔝9953056974 🔝 Escort Service
 
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
VIP Model Call Girls Kothrud ( Pune ) Call ON 8005736733 Starting From 5K to ...
 
Integrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - NeometrixIntegrated Test Rig For HTFE-25 - Neometrix
Integrated Test Rig For HTFE-25 - Neometrix
 
Thermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.pptThermal Engineering -unit - III & IV.ppt
Thermal Engineering -unit - III & IV.ppt
 
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...Bhosari ( Call Girls ) Pune  6297143586  Hot Model With Sexy Bhabi Ready For ...
Bhosari ( Call Girls ) Pune 6297143586 Hot Model With Sexy Bhabi Ready For ...
 
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
Navigating Complexity: The Role of Trusted Partners and VIAS3D in Dassault Sy...
 

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