KEMBAR78
EventHandling in object oriented programming | PPTX
EVENT HANDLING
Event
An event in Java is an object that is created when
something changes within a graphical user interface.
If a user clicks on a button, clicks on a combo box, or
types characters into a text field, etc., then an event
triggers, creating the relevant event object.
Delegation Event Model
a source generates an event and sends it to one or
more listeners. In this scheme, the listener simply
waits until it receives an event. Once received, the
listener processes the event and then returns.
The advantage of this design is that the application
logic that processes events is cleanly separated
from the user interface logic that generates those
events.
In the delegation event model, listeners must register
with a source in order to receive an event
notification.
Event Classes
EventObject is a superclass of all events.
Its Constructor
EventObject(Object src)
Here, src is the object that generates this event.
Its methods
getSource( ): returns the source of the event.
toString( ): returns the string equivalent of the event.
AWTEvent is a superclass of all AWT events that are
handled by the delegation event model.
The package java.awt.event defines several types of
events that are generated by various user interface
elements.
Event Classes in java.awt.event
ActionEvent: Generated when a button is pressed, a
list item is double clicked, or a menu item is
selected.
AdjustmentEvent: Generated when a scroll bar is
manipulated.
ComponentEvent: Generated when a component is
hidden, moved, resized, or becomes visible.
ContainerEvent: Generated when a component is
added to or removed from a container.
FocusEvent: Generated when a component gains or
loses keyboard focus.
InputEvent: Abstract super class for all component
input event classes.
ItemEvent: Generated when a check box or list item is
clicked; also occurs when a choice selection is made
or a checkable menu item is selected or deselected.
KeyEvent: Generated when input is received from the
keyboard.
MouseEvent: Generated when the mouse is dragged,
moved, clicked, pressed, or released; also generated
when the mouse enters or exits a component.
TextEvent: Generated when the value of a text area or
text field is changed.
WindowEvent: Generated when a window is
activated, closed, deactivated, opened, or quit.
Sources of Events
Event Listener Interfaces
the delegation event model has two parts:
sources and listeners.
Listeners are created by implementing one or more
of the interfaces defined by the java.awt.event
package.
When an event occurs, the event source invokes the
appropriate method defined by the listener and
provides an event object as its argument.
List of listener interfaces
ActionListener: Defines one method to receive action
events.
void actionPerformed(ActionEvent ae)
AdjustmentListener: Defines one method to receive
adjustment events.
void adjustmentValueChanged(AdjustmentEvent ae)
ComponentListener: Defines four methods to recognize
when a component is hidden, moved, resized, or shown.
void componentResized(ComponentEvent ce)
void componentMoved(ComponentEvent ce)
void componentShown(ComponentEvent ce)
void componentHidden(ComponentEvent ce)
ContainerListener: Defines two methods to recognize when a
component is added to or removed from a container.
void componentAdded(ContainerEvent ce)
void componentRemoved(ContainerEvent ce)
FocusListener :Defines two methods to recognize when a component
gains or loses keyboard focus.
void focusGained(FocusEvent fe)
void focusLost(FocusEvent fe)
ItemListener: Defines one method to recognize when the state of an
item changes.
void itemStateChanged(ItemEvent ie)
KeyListener: Defines three methods to recognize when a key is pressed, released, or typed.
void keyPressed(KeyEvent ke)
void keyReleased(KeyEvent ke)
void keyTyped(KeyEvent ke)
MouseListener: Defines five methods to recognize when the mouse is clicked, enters a
component, exits a component, is pressed, or is released.
void mouseClicked(MouseEvent me)
void mouseEntered(MouseEvent me)
void mouseExited(MouseEvent me)
void mousePressed(MouseEvent me)
void mouseReleased(MouseEvent me)
MouseMotionListener: Defines two methods to recognize when the mouse is dragged or
moved.
void mouseDragged(MouseEvent me)
void mouseMoved(MouseEvent me)
MouseWheelListener: Defines one method to recognize when the mouse wheel is moved.
void mouseWheelMoved(MouseWheelEvent mwe)
TextListener: Defines one method to recognize when a text value changes.
void textChanged(TextEvent te)
WindowFocusListener: Defines two methods to recognize when a window gains
or loses input focus.
void windowGainedFocus(WindowEvent we)
void windowLostFocus(WindowEvent we)
WindowListener: Defines seven methods to recognize when a window is
activated, closed, deactivated, deiconified, iconified, opened, or quit.
void windowActivated(WindowEvent we)
void windowClosed(WindowEvent we)
void windowClosing(WindowEvent we)
void windowDeactivated(WindowEvent we)
void windowDeiconified(WindowEvent we)
void windowIconified(WindowEvent we)
void windowOpened(WindowEvent we)
Handling Mouse events
import java.awt.*;
import java.awt.event.*;
public class Mouseevents extends Frame implements MouseListener,
MouseMotionListener
{
String msg="";
int x=0,y=0;
Mouseevents()
{
addMouseListener(this );
addMouseMotionListener(this);
}
public static void main(String args[])
{
Mouseevents e=new Mouseevents();
e.setSize(400,400);
e.setLayout(new FlowLayout());
e.setVisible(true);
}
public void mouseClicked(MouseEvent me)
{
x=100;
y=200;
msg="mouse clicked";
repaint();
}
public void mouseEntered(MouseEvent me)
{
x=150;
y=200;
msg="mouse entered";
repaint();
}
public void mouseExited(MouseEvent me)
{
x=100;
y=200;
msg="mouse exited";
repaint();
}
public void mousePressed(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="down";
repaint();
}
public void mouseReleased(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="up";
repaint();
}
public void mouseDragged(MouseEvent me)
{
x=me.getX();
y=me.getY();
msg="*";
System.out.println("dragging mouse at"+x+","+y);
repaint();
}
public void mouseMoved(MouseEvent me)
{
System.out.println("moving mouse at"+me.getX()+","+me.getY());
}
public void paint(Graphics g)
{
g.drawString(msg,x,y);
}
}
Handling Keyboard Events
import java.awt.*;
import java.awt.event.*;
public class KeyEventExample extends Frame implements KeyListener
{
TextField input;
TextField output;
TextArea area;
KeyEventExample()
{
input = new TextField(30);
input.addKeyListener(this);
output = new TextField(30);
output.addKeyListener(this);
area=new TextArea();
area.setBounds(30,80,300,300);
area.addKeyListener(this);
add(input);
add(output);
add(area);
}
public void keyTyped(KeyEvent event)
{
input.setText("Key Typed");
}
public void keyPressed(KeyEvent event)
{
output.setText("Key Pressed");
}
public void keyReleased(KeyEvent event)
{
output.setText("Key Released");
}
public static void main(String args[])
{
KeyEventExample k=new KeyEventExample();
k.setSize(500,500);
k.setLayout(new FlowLayout());
k.setVisible(true);
}
}
Adapter Classes
adapter classes provide the default implementation of
listener interfaces.
If we inherit the adapter class, you will not be forced
to provide the implementation of all the methods of
listener interfaces.
The adapter classes are found in java.awt.event,
java.awt.dnd and javax.swing.event packages
WindowAdapter
import java.awt.*;
import java.awt.event.*;
public class AdapterExample{
Frame f;
AdapterExample(){
f=new Frame("Window Adapter");
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
f.dispose();
}
});
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public static void main(String[] args) {
new AdapterExample();
}
}
MouseAdapter
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseAdapter{
Frame f;
MouseAdapterExample(){
f=new Frame("Mouse Adapter");
f.addMouseListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void mouseClicked(MouseEvent e) {
Graphics g=f.getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
public static void main(String[] args) {
new MouseAdapterExample();
}
}
MouseMotionAdapter
import java.awt.*;
import java.awt.event.*;
public class MouseMotionAdapterExample extends MouseMotionAdapter{
Frame f;
MouseMotionAdapterExample(){
f=new Frame("Mouse Motion Adapter");
f.addMouseMotionListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Graphics g=f.getGraphics();
g.setColor(Color.ORANGE);
g.fillOval(e.getX(),e.getY(),20,20);
}
public static void main(String[] args) {
new MouseMotionAdapterExample();
}
}
KeyAdapter
import java.awt.*;
import java.awt.event.*;
public class KeyAdapterExample extends KeyAdapter{
Label l;
TextArea area;
Frame f;
KeyAdapterExample(){
f=new Frame("Key Adapter");
l=new Label();
l.setBounds(20,50,200,20);
area=new TextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
f.add(l);f.add(area);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void keyReleased(KeyEvent e) {
String text=area.getText();
String words[]=text.split("s");
l.setText("Words: "+words.length+" Characters:"+text.length());
}
public static void main(String[] args) {
new KeyAdapterExample();
}
}

EventHandling in object oriented programming

  • 1.
    EVENT HANDLING Event An eventin Java is an object that is created when something changes within a graphical user interface. If a user clicks on a button, clicks on a combo box, or types characters into a text field, etc., then an event triggers, creating the relevant event object.
  • 2.
    Delegation Event Model asource generates an event and sends it to one or more listeners. In this scheme, the listener simply waits until it receives an event. Once received, the listener processes the event and then returns. The advantage of this design is that the application logic that processes events is cleanly separated from the user interface logic that generates those events. In the delegation event model, listeners must register with a source in order to receive an event notification.
  • 3.
    Event Classes EventObject isa superclass of all events. Its Constructor EventObject(Object src) Here, src is the object that generates this event. Its methods getSource( ): returns the source of the event. toString( ): returns the string equivalent of the event. AWTEvent is a superclass of all AWT events that are handled by the delegation event model. The package java.awt.event defines several types of events that are generated by various user interface elements.
  • 4.
    Event Classes injava.awt.event ActionEvent: Generated when a button is pressed, a list item is double clicked, or a menu item is selected. AdjustmentEvent: Generated when a scroll bar is manipulated. ComponentEvent: Generated when a component is hidden, moved, resized, or becomes visible. ContainerEvent: Generated when a component is added to or removed from a container. FocusEvent: Generated when a component gains or loses keyboard focus.
  • 5.
    InputEvent: Abstract superclass for all component input event classes. ItemEvent: Generated when a check box or list item is clicked; also occurs when a choice selection is made or a checkable menu item is selected or deselected. KeyEvent: Generated when input is received from the keyboard. MouseEvent: Generated when the mouse is dragged, moved, clicked, pressed, or released; also generated when the mouse enters or exits a component. TextEvent: Generated when the value of a text area or text field is changed. WindowEvent: Generated when a window is activated, closed, deactivated, opened, or quit.
  • 6.
  • 7.
    Event Listener Interfaces thedelegation event model has two parts: sources and listeners. Listeners are created by implementing one or more of the interfaces defined by the java.awt.event package. When an event occurs, the event source invokes the appropriate method defined by the listener and provides an event object as its argument.
  • 8.
    List of listenerinterfaces ActionListener: Defines one method to receive action events. void actionPerformed(ActionEvent ae) AdjustmentListener: Defines one method to receive adjustment events. void adjustmentValueChanged(AdjustmentEvent ae) ComponentListener: Defines four methods to recognize when a component is hidden, moved, resized, or shown. void componentResized(ComponentEvent ce) void componentMoved(ComponentEvent ce) void componentShown(ComponentEvent ce) void componentHidden(ComponentEvent ce)
  • 9.
    ContainerListener: Defines twomethods to recognize when a component is added to or removed from a container. void componentAdded(ContainerEvent ce) void componentRemoved(ContainerEvent ce) FocusListener :Defines two methods to recognize when a component gains or loses keyboard focus. void focusGained(FocusEvent fe) void focusLost(FocusEvent fe) ItemListener: Defines one method to recognize when the state of an item changes. void itemStateChanged(ItemEvent ie)
  • 10.
    KeyListener: Defines threemethods to recognize when a key is pressed, released, or typed. void keyPressed(KeyEvent ke) void keyReleased(KeyEvent ke) void keyTyped(KeyEvent ke) MouseListener: Defines five methods to recognize when the mouse is clicked, enters a component, exits a component, is pressed, or is released. void mouseClicked(MouseEvent me) void mouseEntered(MouseEvent me) void mouseExited(MouseEvent me) void mousePressed(MouseEvent me) void mouseReleased(MouseEvent me) MouseMotionListener: Defines two methods to recognize when the mouse is dragged or moved. void mouseDragged(MouseEvent me) void mouseMoved(MouseEvent me) MouseWheelListener: Defines one method to recognize when the mouse wheel is moved. void mouseWheelMoved(MouseWheelEvent mwe)
  • 11.
    TextListener: Defines onemethod to recognize when a text value changes. void textChanged(TextEvent te) WindowFocusListener: Defines two methods to recognize when a window gains or loses input focus. void windowGainedFocus(WindowEvent we) void windowLostFocus(WindowEvent we) WindowListener: Defines seven methods to recognize when a window is activated, closed, deactivated, deiconified, iconified, opened, or quit. void windowActivated(WindowEvent we) void windowClosed(WindowEvent we) void windowClosing(WindowEvent we) void windowDeactivated(WindowEvent we) void windowDeiconified(WindowEvent we) void windowIconified(WindowEvent we) void windowOpened(WindowEvent we)
  • 12.
    Handling Mouse events importjava.awt.*; import java.awt.event.*; public class Mouseevents extends Frame implements MouseListener, MouseMotionListener { String msg=""; int x=0,y=0; Mouseevents() { addMouseListener(this ); addMouseMotionListener(this); } public static void main(String args[]) { Mouseevents e=new Mouseevents(); e.setSize(400,400); e.setLayout(new FlowLayout()); e.setVisible(true); }
  • 13.
    public void mouseClicked(MouseEventme) { x=100; y=200; msg="mouse clicked"; repaint(); } public void mouseEntered(MouseEvent me) { x=150; y=200; msg="mouse entered"; repaint(); } public void mouseExited(MouseEvent me) { x=100; y=200; msg="mouse exited"; repaint(); } public void mousePressed(MouseEvent me) { x=me.getX(); y=me.getY(); msg="down"; repaint(); }
  • 14.
    public void mouseReleased(MouseEventme) { x=me.getX(); y=me.getY(); msg="up"; repaint(); } public void mouseDragged(MouseEvent me) { x=me.getX(); y=me.getY(); msg="*"; System.out.println("dragging mouse at"+x+","+y); repaint(); } public void mouseMoved(MouseEvent me) { System.out.println("moving mouse at"+me.getX()+","+me.getY()); } public void paint(Graphics g) { g.drawString(msg,x,y); } }
  • 15.
    Handling Keyboard Events importjava.awt.*; import java.awt.event.*; public class KeyEventExample extends Frame implements KeyListener { TextField input; TextField output; TextArea area; KeyEventExample() { input = new TextField(30); input.addKeyListener(this); output = new TextField(30); output.addKeyListener(this); area=new TextArea(); area.setBounds(30,80,300,300); area.addKeyListener(this); add(input); add(output); add(area); }
  • 16.
    public void keyTyped(KeyEventevent) { input.setText("Key Typed"); } public void keyPressed(KeyEvent event) { output.setText("Key Pressed"); } public void keyReleased(KeyEvent event) { output.setText("Key Released"); } public static void main(String args[]) { KeyEventExample k=new KeyEventExample(); k.setSize(500,500); k.setLayout(new FlowLayout()); k.setVisible(true); } }
  • 17.
    Adapter Classes adapter classesprovide the default implementation of listener interfaces. If we inherit the adapter class, you will not be forced to provide the implementation of all the methods of listener interfaces. The adapter classes are found in java.awt.event, java.awt.dnd and javax.swing.event packages
  • 19.
    WindowAdapter import java.awt.*; import java.awt.event.*; publicclass AdapterExample{ Frame f; AdapterExample(){ f=new Frame("Window Adapter"); f.addWindowListener(new WindowAdapter(){ public void windowClosing(WindowEvent e) { f.dispose(); } }); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } public static void main(String[] args) { new AdapterExample(); } }
  • 20.
    MouseAdapter import java.awt.*; import java.awt.event.*; publicclass MouseAdapterExample extends MouseAdapter{ Frame f; MouseAdapterExample(){ f=new Frame("Mouse Adapter"); f.addMouseListener(this); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public void mouseClicked(MouseEvent e) { Graphics g=f.getGraphics(); g.setColor(Color.BLUE); g.fillOval(e.getX(),e.getY(),30,30); } public static void main(String[] args) { new MouseAdapterExample(); } }
  • 21.
    MouseMotionAdapter import java.awt.*; import java.awt.event.*; publicclass MouseMotionAdapterExample extends MouseMotionAdapter{ Frame f; MouseMotionAdapterExample(){ f=new Frame("Mouse Motion Adapter"); f.addMouseMotionListener(this); f.setSize(300,300); f.setLayout(null); f.setVisible(true); } public void mouseDragged(MouseEvent e) { Graphics g=f.getGraphics(); g.setColor(Color.ORANGE); g.fillOval(e.getX(),e.getY(),20,20); } public static void main(String[] args) { new MouseMotionAdapterExample(); } }
  • 22.
    KeyAdapter import java.awt.*; import java.awt.event.*; publicclass KeyAdapterExample extends KeyAdapter{ Label l; TextArea area; Frame f; KeyAdapterExample(){ f=new Frame("Key Adapter"); l=new Label(); l.setBounds(20,50,200,20); area=new TextArea(); area.setBounds(20,80,300, 300); area.addKeyListener(this); f.add(l);f.add(area); f.setSize(400,400); f.setLayout(null); f.setVisible(true); } public void keyReleased(KeyEvent e) { String text=area.getText(); String words[]=text.split("s"); l.setText("Words: "+words.length+" Characters:"+text.length()); } public static void main(String[] args) { new KeyAdapterExample(); } }