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

PPTX
Event handling
PDF
12 High Level UI Event Handling
PDF
Ch5 intent broadcast receivers adapters and internet
DOCX
Event oriented programming
PPT
Event+driven+programming key+features
PPTX
Event and signal driven programming
PPTX
Event and signal driven programming techniques
DOCX
Sensor android
Event handling
12 High Level UI Event Handling
Ch5 intent broadcast receivers adapters and internet
Event oriented programming
Event+driven+programming key+features
Event and signal driven programming
Event and signal driven programming techniques
Sensor android

Similar to File Handling (20)

PPTX
tL20 event handling
PDF
java-programming GUI- Event Handling.pdf
PPTX
event-handling.pptx
PPTX
Event Handling in Java
PDF
Event Handling in Java as per university
PPT
Chapter 8 event Handling.ppt m m m m m m
PDF
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
PPTX
Advance java programming- Event handling
PDF
Ajp notes-chapter-03
PPTX
Event Handling in JAVA
PPT
Events1
PPTX
What is Event
PDF
Java Programming :Event Handling(Types of Events)
PPT
event handling new.ppt
PPT
Java gui event
PDF
Event handling
PDF
Event handling
PPTX
OOP Lecture 11-EventHandling1.pptx
PPTX
Chap - 2 - Event Handling.pptx
tL20 event handling
java-programming GUI- Event Handling.pdf
event-handling.pptx
Event Handling in Java
Event Handling in Java as per university
Chapter 8 event Handling.ppt m m m m m m
JAVA PROGRAMMING- GUI Programming with Swing - The Swing Buttons
Advance java programming- Event handling
Ajp notes-chapter-03
Event Handling in JAVA
Events1
What is Event
Java Programming :Event Handling(Types of Events)
event handling new.ppt
Java gui event
Event handling
Event handling
OOP Lecture 11-EventHandling1.pptx
Chap - 2 - Event Handling.pptx
Ad

Recently uploaded (20)

PDF
Prof. Dr. KAYIHURA A. SILAS MUNYANEZA, PhD..pdf
PDF
MLpara ingenieira CIVIL, meca Y AMBIENTAL
PDF
Java Basics-Introduction and program control
PDF
First part_B-Image Processing - 1 of 2).pdf
PPTX
Building constraction Conveyance of water.pptx
PDF
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
PDF
Applications of Equal_Area_Criterion.pdf
PDF
distributed database system" (DDBS) is often used to refer to both the distri...
PPTX
Information Storage and Retrieval Techniques Unit III
PDF
Abrasive, erosive and cavitation wear.pdf
PDF
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
PPTX
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
PDF
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
PDF
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
PPTX
Feature types and data preprocessing steps
PDF
Implantable Drug Delivery System_NDDS_BPHARMACY__SEM VII_PCI .pdf
PPTX
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
PPTX
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
PDF
20250617 - IR - Global Guide for HR - 51 pages.pdf
PPTX
Petroleum Refining & Petrochemicals.pptx
Prof. Dr. KAYIHURA A. SILAS MUNYANEZA, PhD..pdf
MLpara ingenieira CIVIL, meca Y AMBIENTAL
Java Basics-Introduction and program control
First part_B-Image Processing - 1 of 2).pdf
Building constraction Conveyance of water.pptx
Unit I -OPERATING SYSTEMS_SRM_KATTANKULATHUR.pptx.pdf
Applications of Equal_Area_Criterion.pdf
distributed database system" (DDBS) is often used to refer to both the distri...
Information Storage and Retrieval Techniques Unit III
Abrasive, erosive and cavitation wear.pdf
LOW POWER CLASS AB SI POWER AMPLIFIER FOR WIRELESS MEDICAL SENSOR NETWORK
AUTOMOTIVE ENGINE MANAGEMENT (MECHATRONICS).pptx
UEFA_Embodied_Carbon_Emissions_Football_Infrastructure.pdf
Influence of Green Infrastructure on Residents’ Endorsement of the New Ecolog...
Feature types and data preprocessing steps
Implantable Drug Delivery System_NDDS_BPHARMACY__SEM VII_PCI .pdf
Sorting and Hashing in Data Structures with Algorithms, Techniques, Implement...
Graph Data Structures with Types, Traversals, Connectivity, and Real-Life App...
20250617 - IR - Global Guide for HR - 51 pages.pdf
Petroleum Refining & Petrochemicals.pptx
Ad

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