Thursday, August 3, 2023

Java Event Handling

 

Event Handling


The modern approach to handling event is based on the delegation event model,which defines standard and consistent mechanisms  to generate  and process events. its concept is quite simple:a source generates an event and sends it to one or more listeners.

Events

In the delegation model,an event is an object that describes a state change in a source.it can be generated as a consequence of a person interacting with the elements in a graphical user interface.

Event source

*A source is an object that generates an event.

Event Listeners

A listeners is an object that is notified when an event occurs.

       The methods that receive and process events are defined  in a set of interfaces found in java.awt.event.

   EVENT CLASS

  DESCRIPTION

ActionEvent

Generated when a button is pressed, a list item is double clicked, or a menu item is selected

Adjustment Event

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.

Focus Evevt

Generated when a component gain 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,deiconified,

Iconified,opened,or quit

 

Event Listeners

   EVENT CLASS

  DESCRIPTION

Action Listeners

Define one method to receive action events 

Adjustment Listeners

Define one method to receive adjustment events.

Component Listeners

Define four methods to recognize when a component is hidden,moved,resized,or shown

Container Listeners

Define two methods to recognize when a component is added to or removed from a container

Focus Listeners

Define two methods to recognize when a component gain or loses keyboard focus.

Item Listeners

Define one methods to recognize when

the state of an item changes.

Key Listeners

Define three methods to recognize when a key is pressed,released,or typed.

Mouse Listeners

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

MouseMotion Listeners

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

Text Listeners

Define one methods to recognize when

a text value changes

WindowFocus Listeners

Define two methods to recognize when

a window gain or loses input focus.

Window Listeners

Define two methods to recognize when

a window is activated,closed,deactivated,deiconified,iconified,opened,or quit.

 

Mouse Clicks

Mouse-Click events occurs when the user clicks the mouse somewhere in the body of the applet.we can intercept mouse clicks to do very simple things-for example,to toggle the sound on and off in the applet,to move to the next slide in a presentation,or to clear the screen and start over.

The MouseListenerInterface

The general forms of these methods are shown here:

void mouseClicked(MouseEvent me)

void mouseEntered(MouseEvent me)

void mouseExited(MouseEvent me)

void mousePressed(MouseEvent me)

void mouseReleased(MouseEvent me)

The MouseMotionListenerInterface

void mouseDragged(MouseEvent me)

void mouseMoved(MouseEvent me)

Handling Mouse Event

Example: 

File name: MouseEvents.java


import java.awt.*;

import java.awt.event.*;

import java.applet.*;

/*

<applet code="MouseEvents"width=300 height=100>

</applet>

*/

public class MouseEvents extends Applet

implements MouseListener,MouseMotionListener

{

String msg="";

int mouseX=0,mouseY=0;//coordinates of mouse

public void init()

{

addMouseListener(this);

addMouseMotionListener(this);

}

//Handle mouse clicked.

public void mouseClicked(MouseEvent me){

//save coordinates

mouseX=0;

mouseY=10;

msg="Mouse clicked.";

repaint();

}

//Handle mouse entered.

public void mouseEntered(MouseEvent me)

{

//save coordinates

mouseX=0;

mouseY=10;

msg="Mouse entered.";

repaint();

}

//Handle mouse exited.

public void mouseExited(MouseEvent me)

{

//save coordinates

mouseX=0;

mouseY=10;

msg="Mouse exited.";

repaint();

}

//Handle button pressed.

public void mousePressed(MouseEvent me)

{

//save coordinates

mouseX=me.getX();

mouseY=me.getY();

msg="Down";

repaint();

}

//Handle button released.

public void mouseReleased(MouseEvent me)

{

//save coordinates

mouseX=me.getX();

mouseY=me.getY();

msg="Up";

repaint();

}

//Handle mouse dragged.

public void mouseDragged(MouseEvent me)

{

//save coordinates

mouseX=me.getX();

mouseY=me.getY();

msg="*";

showStatus("Dragging mouse at"+mouseX+","+mouseY);

repaint();

}

//Handle mouse moved.

public void mouseMoved(MouseEvent me)

{

//show status

showStatus("Moving mouse at"+me.getX()+","+me.getY());

}

//Display msg in applet window at current X,Y location.

public void paint(Graphics g)

{

g.drawString(msg,mouseX,mouseY);

}

}


Output

 




Keyboard Event

Keyboard Events

Keyboard events are generated whenever the users press a key on the keyboard.

When a key is pressed,a KEY_PRESSED event is generated.

This result in a call to the KeyPressed( ) event handler.When the key is released,a KEY_RELEASED event is generated and the KeyReleased( ) handler is executed.if a character is generated by the Keystroke,then a KEY_TYPED event is sent and the KeyTyped( )handler is invoked.

File name: SimpleKey.java

import java.awt.*;

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){

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

}

}


Output

 




Adapter Classes

java provides a special feature,called an adapter class that can simplify the creation of event handlers in certain sitiuations.

An adapter class provides an empty implementation of all methods in an event listener interface.

Adapter Class

Listener Interface

ComponentAdapter

ComponentListener

ContainerAdapter

ContainerListener

FocusAdapter

FocusListener

KeyAdapter

KeyListener

MouseAdapter

MouseListener

MouseMotionAdapter

MouseMotionListener

WindowAdapter

WindowListener

Example 1:

File name: MouseAdapter1.java

import java.awt.*;   

import java.awt.event.*;   

 

public class MouseAdapter1 extends MouseAdapter {   

    Frame f;   

    MouseAdapter1() {   

        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.GREEN);    

        g.fillOval (e.getX(), e.getY(), 30, 30);   

    }   

public static void main(String[] args) {   

    new MouseAdapter1();   

}   

}   


Output

 



Example 2

File name: KeyAdapterExample.java

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

    }   

}   


Output



Example 3:

File name: MouseAdapterDemo.java

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

import java.awt.event.MouseListener;

/*<applet code="MouseAdapterDemo" width=100 height=100>

</applet>*/

public class MouseAdapterDemo extends Applet

{

public void init()

{

setBackground(Color.green);

}

}

File name: MyMouseAdapter.java

import java.applet.*;

import java.awt.*;

import java.awt.event.*;

class MyMouseAdapter extends MouseAdapterDemo

{

MouseAdapterDemo m;

public MyMouseAdapter(MouseAdapterDemo m)

{

this.m=m;

}

public void mousePressed(MouseEvent me)

{

m.setBackground(Color.red);

m.repaint();

}

public void mouseReleased(MouseEvent me)

{

m.setBackground(Color.green);

m.repaint();

}

}


Output

 


 

 

0 comments:

Post a Comment