You are on page 1of 22

Event Handling

Older computing models revolved about


the console input and output.
In modern days we use GUI and various
components like buttons, scroll bars, and
textboxes.
Programming model has also changed to
event driven programming.
It has made events very important.

Event
Interfaces

ActionListener

Defines one method to receive action events.

AdjustmentListener
Defines one method to receive adjustment events.

ComponentListener
Defines four methods to recognize when a component is hidden,
moved, resized, or shown.

ContainerListener
Defines two methods to recognize when a component is added
to or removed from a container.

FocusListener
Defines two methods to recognize when a Component gains or
lose keyboard focus.

ItemListener
Defines one method to recognize when the state of an item
changes.

Event Interfaces
KeyListener

Defines three methods to recognize when a key is pressed,


released, or typed.

MouseListener
Defines five methods to recognize when the mouse is clicked,
enters a component, exits a component, is pressed, or is
released.

MouseMotionListener
Defines two methods to recognize when the mouse is dragged
or moved.

TextListener
Defines one method to recognize when a text value changes.

WindowListener
Defines seven methods to recognize when a window is
activated, closed, deactivated, deiconified, iconified, opened, or
quit.

Event Classes
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 lose keyboard focus.

InputEvent
Abstract super class for all component input event classes.

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,
deiconified, iconified, opened, or quit.

Using Button Example 1


import java.awt.*;
import java.awt.event.*;
public class Button1 extends Frame implements ActionListener{
Button bt;
public Button1()
{

super("Button");
setSize(200, 300);
bt = new Button("Exit");
add(bt);
bt.addActionListener(this);
setLayout(new GridLayout());
setVisible(true);

}
public void actionPerformed(ActionEvent ev) {
String st = ev.getActionCommand();
if ( st.equals("Exit")) {
System.exit(0);
}

Button using Applet Example 2


import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*<applet code="Button2" width=300 height=300></applet>*/
public class Button2 extends Applet implements ActionListener
{

Button bt,bt1;
String str;
public void init()
{

bt = new Button("JAVA");
add(bt);
bt.addActionListener(this);
bt1 = new Button("Exit");
add(bt1);

bt1.addActionListener(this);
setLayout(new FlowLayout());

Example (cont)
public void actionPerformed(ActionEvent ev)
{
if (ev.getSource()==bt) {
str=bt.getLabel();
str+=" @ IMAGE";
repaint();
}
if (ev.getSource()==bt1) {
System.exit(0);
}
}
public void paint(Graphics g)
{
g.drawString(str,20,100);
}
}

Using TextField Example 3


import java.awt.*;
import java.awt.event.*;
public class Textfil extends Frame implements ActionListener
{ TextField tx1, tx2;
Button bt;
public Textfil()
{ super("The text demo");
setSize(200, 300);
tx1 = new TextField(10);
add(tx1);
tx2 = new TextField(10);
add(tx2);
bt = new Button("Copy");
add(bt);
bt.addActionListener(this);
setLayout(new FlowLayout());
setVisible(true);
this.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent ae)
{
System.exit(0);
}
});
}

Example (Cont)
public void actionPerformed(ActionEvent ev)
{
String st = ev.getActionCommand();
if (st.equals("Copy"))
{ tx2.setText(tx1.getText());
}
}
public static void main(String[] args)
{ Textfil t = new Textfil();
}
}//end of class

Key Event Example 4


import java.awt.*;

import java.awt.event.*
public class KeyEventDemo extends Frame implements KeyListener
{ public static String str1 = "", str2 = "", str3="";
public static Graphics myG;
public KeyEventDemo()
{ super("Key");
setSize(500, 500);
setVisible(true);
myG = getGraphics();
}
public void keyTyped(KeyEvent ke)
{ char ch;
ch = ke.getKeyChar();
str1 = str1 + ch;
myG.drawString(str1, 50, 100);
}
public void keyPressed(KeyEvent ke)
{ int n;
n = ke.getKeyCode();
str2 = str2 + n+ "";
myG.drawString(str2, 50, 200);
}

Example (Cont)
public void keyReleased(KeyEvent ke)
{ int n;
n = ke.getKeyCode();
str3 = str3 + n + "";
myG.drawString(str3, 50, 300);
}
public void paint(Graphics myG)
{ this.addKeyListener(this);
Font f1 = new Font("Arial", Font.BOLD, 20);
myG.setFont(f1);
myG.drawString("Key Typed" , 50, 50);
myG.drawString("Key pressed", 50, 150);
myG.drawString("Key released", 50, 250);
Font f2 = new Font("Arial", Font.BOLD, 16);
myG.setFont(f2);
}
public static void main(String[] args)
{ KeyEventDemo ked = new KeyEventDemo();
}
}//end of class

Mouse Event Example 5


import java.awt.*;
import java.awt.event.*;
public class MouseEvents
{ public static void main(String args[])
{ Frame f= new Frame();
f.setLayout(new FlowLayout());
f.setSize(300,300);
Button b= new Button("Push me");
b.addMouseListener(new MyMouseListener());
b.addMouseMotionListener(new MyMouseMotionListener());
b.addActionListener(new MyActionListener());
f.add(b);
f.setVisible(true);
}
}

Example (Cont)
class MyMouseListener implements MouseListener
{ public void mouseEntered (MouseEvent e)
{ System.out.println("Mouse Entered");
}
public void mouseExited(MouseEvent e)
{ System.out.println("Mouse Exited");
}
public void mousePressed(MouseEvent e)
{ System.out.println("Mouse Pressed");
}
public void mouseReleased(MouseEvent e)
{ System.out.println("Mouse Released");
}
public void mouseClicked(MouseEvent e)
{ System.out.println("Mouse Clicked");
}
}

Example (Cont)
class MyMouseMotionListener implements
MouseMotionListener
{ public void mouseMoved(MouseEvent e)
{ System.out.println("Mouse moved");
}
public void mouseDragged(MouseEvent e)
{ System.out.println("Mouse Dragged");
}
}
class MyActionListener implements ActionListener
{ public void actionPerformed(ActionEvent ae)
{ System.out.println("Button pressed");
}
}

ItemEvent Example 6
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/*<applet code="ItemListenerDemo" width=200 height=200></applet>*/
public class ItemListenerDemo extends Applet implements ItemListener
{
String str;
Label label;
Checkbox b1,b2,b3;
public void init()
{
label=new Label("Select Any Course at IMAGE");
b1=new Checkbox("C/CPP");
b2=new Checkbox("PHP");
b3=new Checkbox("JAVA");
add(b1); add(b2); add(b3);
b1.addItemListener(this);
b2.addItemListener(this);
b3.addItemListener(this);
}

Example (Cont)
public void itemStateChanged(ItemEvent ae)
{
str=((Checkbox)ae.getItemSelectable()).getLabel();
repaint();
}
public void paint(Graphics g)
{
g.drawString("Course Selected
}
}//end of class

"+ str, 40,100);

Adapter class
The main drawback to implement a listener is we need
to define all the method declared in the interface.
For example if are implementing MouseListener we
need to implement all of its method, if we require only
one method mouseClicked() then we need to define all
of its interfaces method unnecessary.
To prevent this Java provides the concept of the
Adapter classes.
Adapter classes are the dummy classes.
It means it implements all method of a given interface
without any action.
Instead of implementing an interface, we should extend
the respective adapter class.
In that case we have to define only one or two required
methods. No other methods needs to be defined.

Example
import java.awt.*;
import java.awt.event.*;
import java.applet.*;

/*
<applet code ="AdapterDemo" width = 300 height= 100>
</applet>
*/

public class AdapterDemo extends Applet


{
public void init()
{
addMouseListener (new MyMouseAdapter(this));
addMouseMotionListener(new MyMouseMotionAdapter(this));
}
}

class MyMouseAdapter extends MouseAdapter


{

AdapterDemo adapterDemo;
public MyMouseAdapter (AdapterDemo adapterDemo)
{

this.adapterDemo=adapterDemo;

}
//Handle mouse clicked.
public void mouseClicked(MouseEvent me)
{

adapterDemo.showStatus("Mouse clicked");

}
}
class MyMouseMotionAdapter extends MouseMotionAdapter
{

AdapterDemo adapterDemo;
public MyMouseMotionAdapter(AdapterDemo adapterDemo)
{

this.adapterDemo=adapterDemo;

}
public void mouseDragged (MouseEvent me)

List of Adapter Classes


Adapter Classes
Listener Interface
MouseAdapter
MouseListener
MouseMotionAdapter MouseMotionListener
KeyAdapter
KeyListener
WindowAdapter
WindowListener
ComponentAdapter
ComponentListener
ContainerAdapter ContainerListener
FacusAdapter
FocusListener

Drawing Pad Example


import java.awt.*;

import java.awt.event.*;
public class DrawingPadDemo extends MouseMotionAdapter
{ Frame f;
public DrawingPadDemo()
{ f= new Frame();
f.setSize(400,400);
f.setVisible(true);
f.addMouseMotionListener(this);
}
public void mouseDragged(MouseEvent me)
{ Graphics g= f.getGraphics();
g.drawLine(me.getX(), me.getY(),me.getX(), me.getY());
}
public static void main(String[] args)
{
new DrawingPadDemo();
}
}

You might also like