Let Us Know About Mouse Events And Keyboard Events Handling In Java


This resource contains the information about mouse events and keyboard events handling, adapter class in java etc.

Let Us Know About Mouse Events And Keyboard Events Handling In Java


Mouse Events Handling


MouveEvents.javaButtonInstancemouse

MouseListener, MouseMotionListener interfaces are used for mouse events handling.

MouseListener interface contains five methods:
1. mouseEntered() : It is operated when mouse cursor comes on registered component.
2. mouseExited() : It is operated when mouse cursor goes out of registered component.
3. mousePressed() : It is operated when any mouse button is pressed when mouse cursor is on registered component.
4. mouseClicked() : It is operated when any mouse button is pressed and released at same location when mouse cursor is on the registered component.
5. mouseReleased() : It is operated when any mouse button is pressed at one location and released at another location when mouse cursor is on the registered component.

MouseMotionListener interface contains two methods:
1. mouseMoved() : It is operated when mouse is moved without pressing any mouse button.
2. mouseDragged() : It is operated when mouse is moved by keep pressing any mouse button.
All above seven methods are overridable and contain MouseEvent class type of argument. This argument contains x, y coordinate values when mouse cursor is on the registered component. Its getx() method is used to get x-coordinate value and its gety() method is used to get y- coordinate value.
addMouseListener() method registers component to MouseListener interface.
addMouseMotionListener() method registers component to MouseMotionListener interface.

Keyboard events handling


KeyEvents.java

KeyListener interface is used for keyboard events handling. Keys of keyboard are two types
1. Virtual keys
2. Non-virtual keys


KeyListener contains three methods:
1. keyPressed() : It is used when any key is pressed from keyboard. Normally it contains code for handling virtual keys.
2. keyTyped() : It is operated when non-virtual key is pressed from keyboard. But this method is operated immediately after keyPressed().
3. keyReleased() : It is operated when any key is released from keyboard.
All these methods contain KeyEvent type of argument. getKeyCode() method of KeyEvent class returns number of pressed key and getKeyChar()method of KeyEvent class returns non-virtual key.
KeyEvent class contains static constants for all keys
Example: VK_F1, VK_F2, . . .
addKeyListener() method registers component to KeyListener interface.
showStatus() method displays given message on the status bar of window.

Example: Demonstrate an adaptor.

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class AdapterDemo extends Applet
{
public void init()
{
MyMouseAdapter obj = new MyMouseAdapter(this);
//Here this keyword creates instance of AdapterDemo addMouseListener(obj);
//Applet is registered to MouseAdapter class
addMouseMotionListener(new MouseMotionAdapter(this));
//Applet is registered to MouseMotionAdapter class
}
}
class MyMouseAdapter extends MouseAdapter
{
AdapterDemo adapterDemo; MyMouseAdapter(AdapterDemo adapterDemo)
{
this.adapterDemo=adapterDemo;
}

// Handle mouse clicked.
public void mouseClicked(MouseEvent me)
{
adapterDemo.showStatus("Mouse clicked");
}
public void mouseEntered(MouseEvent me)
{
adapterDemo.showStatus("Mouse Entered");
}
}

class MyMouseMotionAdapter extends MouseMotionAdapter
{
AdapterDemo adapterDemo;
public MyMouseMotionAdapter(AdapterDemoadapterDemo)
{
this.adapterDemo = adapterDemo;
}
// Handle mouse dragged.
public void mouseDragged(MouseEvent me)
{ adapterDemo.showStatus("Mouse dragged");
}
}

/* AdapterClasses:

These are replacement of Listener Interfaces.

Listener Interface:- Adapter Class

MouseListener:- MouseAdapter
MouseMotionListener:- MouseMotionAdapter
KeyListener:- KeyAdapter
WindowListener:- WindowAdapter

In case of Listener interfaces all methods have to be defined, though not required. In case of Adapter classes required method is defined.


Comments

No responses found. Be the first to comment...


  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: