Professional Documents
Culture Documents
Chapter 2
Event handeling
-by-
Prof. Bhandare P. S.
SVERI’s COE(Poly), Pandharpur
Syllabus
Prashant bhandare
Assignment No. 3.
About title of chapter
Prashant bhandare
Importance of chapter
This chapter is important because in this
chapter students are going to learn various
event handling classes and interfaces used to
handle events generated by component.
Prashant bhandare
Objectives of chapter:
By studying this chapter students are able to
1. Use delegation event model to develop event
driven program for the given problem.
2. Use relevant AWT/ swing component(s) to
handle the given event.
3. Use Adapter classes in Java program to solve the
given problem.
4. Use inner classes in java program to solve the
given problem.
Event Handling
Prashant bhandare
Button
◦ public void addActionListener(ActionListener a){}
MenuItem
◦ public void addActionListener(ActionListener a){}
TextField
◦ public void addActionListener(ActionListener a){}
◦ public void addTextListener(TextListener a){}
TextArea
Prashant bhandare
Checkbox
◦ public void addItemListener(ItemListener a){}
Choice
◦ public void addItemListener(ItemListener a){}
List
◦ public void addActionListener(ActionListener a){}
◦ public void addItemListener(ItemListener a){}
Event Sources
Prashant bhandare
register.
◦ EventObject(Object src)
MouseEvent Generated when the mouse is dragged, moved, clicked, pressed, or released; also
generated when the mouse enters or exits a component.
MouseWheelEvent Generated when the mouse wheel is moved.
TextEvent Generated when the value of a text area or text field is changed.
An ActionEvent is generated when a button is pressed, a
list item is double-clicked, or a menu item is selected.
It is shown here:
int getStateChange( )
AdjustmentEvent
Prashant bhandare
char getKeyChar( )
int getKeyCode( )
the event.
It is a subclass of MouseEvent.
MouseWheelEvent:
◦ MouseWheelEvent(Component src, int type, long
when, int modifiers, int x, int y, int clicks, boolean
triggersPopup, int scrollHow, int amount, int count)
Component getComponent( )
The ContainerEvent Class
Prashant bhandare
Component getChild( )
The TextEvent Class
Prashant bhandare
boolean isTemporary( )
BUTTON1_MASK CTRL_MASK
However,
Prashant bhandare because of possible conflicts
between the modifiers used by keyboard
events and mouse events, and other issues,
the following extended modifier values were
added:
BUTTON1_DOWN_MASK CTRL_DOWN_MASK
It is shown here:
int getModifiers( )
ContainerAdapter ContainerListener
FocusAdapter FocusListener
KeyAdapter KeyListener
MouseMotionAdapter MouseMotionListener
So it saves code.
Adapter claso5806
java.awt.dnd Adapter classes
Prashant bhandare
DragSourceAdapter DragSourceListener
DragTargetAdapter DragTargetListener
MouseInputAdapter MouseInputListener
InternalFrameAdapter InternalFrameListener
Java WindowAdapter Example
Prashant bhandare
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();
}
}
import java.awt.*;
Prashant bhandare
import java.awt.event.*;
public class windowAdapterDemo extends
WindowAdapter
{
Frame f;
public windowAdapterDemo()
{
f=new Frame("windowAdapterDemo");
f.setSize(300,300);
f.setVisible(true);
f.addWindowListener(this);
}
public void windowClosing(WindowEvent
Prashant bhandare
we)
{
f.dispose();
}
public static void main(String arr[])
{
new windowAdapterDemo();
}
}
Java MouseAdapter Example
Prashant bhandare
import java.awt.*;
import java.awt.event.*;
public class MouseAdapterExample extends MouseA
dapter{
Frame f;
MouseAdapterExample(){
f=new Frame("Mouse Adapter");
f.addMouseListener(this);
f.setSize(300,300);
f.setLayout(null);
f.setVisible(true);
}
Prashant bhandare
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();
}
}
Java MouseMotionAdapter Example
Prashant bhandare
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);
}
Prashant bhandare
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);
}
Prashant bhandare
import java.applet.*;
import java.awt.event.*;
/*
<applet code="AnonymousInnerClassDemo" width=200
height=100>
</applet>
*/
public class AnonymousInnerClassDemo extends Applet {
public void init() {
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent me) {
showStatus("Mouse Pressed");
}
});
}
}
There is one top-level class in this program:
Prashant bhandare
AnonymousInnerClassDemo.
import java.awt.event.*;
public class JScrollbardemo extends JFrame
implements AdjustmentListener {
JScrollBar js;
JLabel jl;
JScrollbardemo()
{setTitle("JScrollbardemo");
setSize(400,400);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(null);
js=new JScrollBar();
jl=new JLabel();
js.setBounds(20,20,30,250);
jl.setBounds(70,50,130,30);
Prashant bhandare
add(jl);
add(js);
js.setBlockIncrement(90);
js.addAdjustmentListener(this);
}
public void
adjustmentValueChanged(AdjustmentEvent ae)
{
jl.setText("Changed: "+js.getValue());
}
public static void main(String arr[])
{
new JScrollbardemo();
}
}
The ComponentListener Interface
Prashant bhandare
import java.awt.event.*;
import javax.swing.*;
public class focuslistenerdemo extends JFrame implements
FocusListener {
public focuslistenerdemo() {
setTitle("containerdemo");
setVisible(true);
setSize(400,400);
setLayout(null);
JButton jb=new JButton("ok");
jb.setBounds(20,60,100,30);
JTextField jf=new JTextField(20);
jf.setBounds(20,110,100,30);
JTextArea ta=new JTextArea();
ta.setBounds(20,170,100,30);
add(jb);
Prashant bhandare
add(jf);
jb.addFocusListener(this);
jf.addFocusListener(this);
}
public void focusGained(FocusEvent e)
{
System.out.print("\nGained
Focus"+e.getComponent().getClass().getName());
}
public void focusLost(FocusEvent e)
{
System.out.print("\nLost
Focus"+e.getComponent().getClass().getName());
}
public static void main(String arr[])
{
new focuslistenerdemo();
}
}
The ItemListener Interface
Prashant bhandare
import java.awt.event.*;
public class jtogglebuttondemo extends JFrame implements
ItemListener {
JToggleButton jtb;
JLabel jl;
public jtogglebuttondemo() {
setSize(400,400);
setVisible(true);
setTitle("JToggleButton Demo");
setLayout(null);
jtb=new JToggleButton("OK",true);
jtb.setBounds(10,100,150,50);
jl=new JLabel("Change");
jl.setBounds(10,150,250,250);
Prashant bhandare add(jtb);
add(jl);
jtb.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
if(jtb.isSelected())
{
jl.setIcon(new ImageIcon("Penguins.jpg"));
}
else {
jl.setText("Changed");
}
}
public static void main(String arr[]) {
new jtogglebuttondemo();
}
}
The KeyListener Interface
Prashant bhandare
import java.awt.event.*;
import java.applet.*;
/* <applet code="SimpleKey" width=300
height=100> </applet> */
public class SimpleKey extends Applet
implements KeyListener
{
String msg = "";
int X = 10, Y = 20;
public void init()
{
addKeyListener(this);
requestFocus();
}
public void keyPressed(KeyEvent ke) {
Prashant bhandare
showStatus("Key Down");
}
public void keyReleased(KeyEvent ke) {
showStatus("Key Up");
}
public void keyTyped(KeyEvent ke) {
msg += ke.getKeyChar();
repaint();
}
public void paint(Graphics g) {
g.drawString(msg, X, Y);
}
}
The MouseListener Interface
Prashant bhandare
import java.awt.event.*;
public class MouseListenerExample extends Frame implements
MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
l.setText("Mouse Clicked");
}
Prashant bhandare
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
The MouseMotionListener Interface
Prashant bhandare
import java.awt.event.*;
public class MouseMotionListenerExample
extends Frame implements
MouseMotionListener {
Label l;
MouseMotionListenerExample(){
addMouseMotionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Prashant bhandare
Graphics g=getGraphics();
g.setColor(Color.red);
g.fillOval(e.getX(),e.getY(),5,5);
}
public void mouseMoved(MouseEvent me)
{
System.out.print("Mouse MOved");
}
public static void main(String[] args) {
new MouseMotionListenerExample();
}
}
The MouseWheelListener Interface
Prashant bhandare
import java.awt.event.*;
public class mousewheelevent extends
Frame implements MouseWheelListener
{
public mousewheelevent()
{
addMouseWheelListener(this);
setTitle("mousewheelevent Demo");
setSize(400,400);
setVisible(true);
}
public void
Prashant bhandare
mouseWheelMoved(MouseWheelEvent me)
{
System.out.print("\n wheel moving");
}
public static void main(String arr[])
{
new mousewheelevent();
}
}
The TextListener Interface
Prashant bhandare
import java.awt.event.*;
class TextEventExample extends Frame
implements TextListener
{
TextField jt;
TextEventExample()
{
setVisible(true);
setTitle("TextListenerExample");
setSize(500,500);
setLayout(null);
jt=new TextField("Enter name");
jt.setBounds(20,20,100,30);
add(jt);
Prashant bhandare
jt.addTextListener(this);
}
public void textValueChanged(TextEvent te)
{
setTitle(jt.getText());
}
public static void main(String arr[])
{
new TextEventExample();
}
}
The WindowFocusListener Interface
Prashant bhandare
windowGainedFocus( )
windowLostFocus( ).
These are called when a window gains or
loses input focus.
import java.awt.event.*;
public class WindowfocusExample extends
Frame implements WindowFocusListener{
WindowfocusExample(){
addWindowFocusListener(this);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new WindowfocusExample();
}
public void windowGainedFocus(WindowEvent
Prashant bhandare
we)
{
System.out.print("\nWindow focus gained....");
}
public void windowLostFocus(WindowEvent
we)
{
System.out.print("\nWindow focus Lost....");
}
}
The WindowListener Interface
Prashant bhandare
System.out.println("closing");
dispose();
}
public void windowDeactivated(WindowEvent arg0) {
System.out.println("deactivated");
}
public void windowDeiconified(WindowEvent arg0) {
System.out.println("deiconified");
}
public void windowIconified(WindowEvent arg0) {
System.out.println("iconified");
}
public void windowOpened(WindowEvent arg0) {
System.out.println("opened");
}
}
Handling Keyboard Events
Prashant bhandare
{
showStatus("Key Down");
int key = ke.getKeyCode();
switch(key)
{
case KeyEvent.VK_F1:
msg += "<F1>";
break;
case KeyEvent.VK_F2:
msg += "<F2>";
break;
case KeyEvent.VK_F3:
msg += "<F3>";
break;
case KeyEvent.VK_PAGE_DOWN:
Prashant bhandare
msg += "<PgDn>";
break;
case KeyEvent.VK_PAGE_UP:
msg += "<PgUp>";
break;
case KeyEvent.VK_LEFT:
msg += "<Left Arrow>";
break;
case KeyEvent.VK_RIGHT:
msg += "<Right Arrow>";
break;
}
repaint();
}
Java ActionListener Interface
Prashant bhandare
import java.awt.event.*;
public class MouseListenerExample extends
Frame implements MouseListener{
Label l;
MouseListenerExample(){
addMouseListener(this);
l=new Label();
l.setBounds(20,50,100,20);
add(l);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
Prashant bhandare
l.setText("Mouse Clicked");
}
public void mouseEntered(MouseEvent e) {
l.setText("Mouse Entered");
}
public void mouseExited(MouseEvent e) {
l.setText("Mouse Exited");
}
public void mousePressed(MouseEvent e) {
l.setText("Mouse Pressed");
}
public void mouseReleased(MouseEvent e) {
l.setText("Mouse Released");
}
public static void main(String[] args) {
new MouseListenerExample();
}
}
import java.awt.*;
Prashant bhandare
import java.awt.event.*;
public class MouseListenerExample2
extends Frame implements MouseListener{
MouseListenerExample2(){
addMouseListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseClicked(MouseEvent e) {
Prashant bhandare
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),30,30);
}
public void mouseEntered(MouseEvent e) {}
public void mouseExited(MouseEvent e) {}
public void mousePressed(MouseEvent e) {}
public void mouseReleased(MouseEvent e) {}
public static void main(String[] args) {
new MouseListenerExample2();
}
}
Java MouseMotionListener Interface
Prashant bhandare
import java.awt.event.*;
public class MouseMotionListenerExample
extends Frame implements
MouseMotionListener{
MouseMotionListenerExample(){
addMouseMotionListener(this);
setSize(300,300);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e)
Prashant bhandare {
Graphics g=getGraphics();
g.setColor(Color.BLUE);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e) {}
public static void main(String[] args) {
new MouseMotionListenerExample();
}
}
import java.awt.*;
Prashant bhandare
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionListener;
public class Paint extends Frame implements
MouseMotionListener{
Label l;
Color c=Color.BLUE;
Paint(){
l=new Label();
l.setBounds(20,40,100,20);
add(l);
addMouseMotionListener(this);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void mouseDragged(MouseEvent e) {
Prashant bhandare
l.setText("X="+e.getX()+", Y="+e.getY());
Graphics g=getGraphics();
g.setColor(Color.RED);
g.fillOval(e.getX(),e.getY(),20,20);
}
public void mouseMoved(MouseEvent e) {
l.setText("X="+e.getX()+", Y="+e.getY());
}
public static void main(String[] args) {
new Paint();
}
}
Java ItemListener Interface
Prashant bhandare
itemStateChanged() method
l.setText("Key Pressed");
}
public void keyReleased(KeyEvent e) {
l.setText("Key Released");
}
public void keyTyped(KeyEvent e) {
l.setText("Key Typed");
}
public static void main(String[] args) {
new KeyListenerExample();
}
}
import java.awt.*;
Prashant bhandare
import java.awt.event.*;
public class KeyListenerExample extends Frame implements
KeyListener{
Label l;
TextArea area;
KeyListenerExample(){
l=new Label();
l.setBounds(20,50,200,20);
area=new TextArea();
area.setBounds(20,80,300, 300);
area.addKeyListener(this);
add(l);add(area);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public void keyPressed(KeyEvent e) {}
Prashant bhandare
windowClosing() method
There are many ways to override
windowClosing() method:
By anonymous class
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowExample extends Frame{
WindowExample(){
addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
dispose();
}
});
setSize(400,400);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new WindowExample();
}
Example 2: extending WindowAdapter
Prashant bhandare
import java.awt.*;
import java.awt.event.*;
public class AdapterExample extends WindowAdapter{
Frame f;
AdapterExample(){
f=new Frame();
f.addWindowListener(this);
f.setSize(400,400);
f.setLayout(null);
f.setVisible(true);
}
public void windowClosing(WindowEvent e) {
f.dispose();
}
public static void main(String[] args) {
new AdapterExample();
}
}
Example 3: implementing WindowListener
Prashant bhandare
import java.awt.*;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
public class WindowExample extends Frame implements
WindowListener{
WindowExample(){
addWindowListener(this);
setSize(400,400);
setLayout(null);
setVisible(true);
}
public static void main(String[] args) {
new WindowExample();
}
public void windowActivated(WindowEvent e) {}
Prashant bhandare