Home arrow Java & J2EE arrow Page 6 - Event Handling In Java

Naughty Examples For Those Little Mice - Java

In life, you encounter events that force you to suspend other activities and respond to them immediately. In Java, events represent all activity that goes on between the user and the application. Java's Abstract Windowing Toolkit (AWT) communicates these actions to the programs using events.

TABLE OF CONTENTS:
  1. Event Handling In Java
  2. Going Into Over Drive
  3. Relax Back Event Handling In A Nut Shell
  4. Event-Handling In Java
  5. Amazingly Simple Applets
  6. Naughty Examples For Those Little Mice
  7. A Quick Recap
By: Gayathri Gokul
Rating: starstarstarstarstar / 196
September 11, 2003

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement
Mouse Events, MouseListener and MouseMotionListener:

These are generated when a mouse is moved, clicked, pressed and released etc. The MouseEvent class represents these events as six constant values and has methods to get the co-ordinates at which a mouse event occurred. The mouse move and mouse drag are treated differently from the rest four mouse pressed, mouse released, mouse entered and mouse exited. Hence there are two types of mouse event listeners- MouseListener and MouseMotionListener.

The following code illustrates the usage of mouse event listeners. Watch out for messages on the status line of applet as and when the user performs a mouse event. (Example 3 also shows the co-ordinates at which the event occurred.

// Example 3: MouseEvents & MouseListener
/*
<Applet code = "mouseEvent.class" height= 400 width = 400 >
</applet>
*/
Import java.awt.*;
Import java.awt.event.*;
Import javax.swing.*;
Public class mouseEvent extends JApplet implements MouseListener, MouseMotionListener
{
Public void init()
{
addMouseListener(this);
addMouseMotionListener(this);
}
Public void mouseClicked(MouseEvent e)
{
showStatus("Mouse has been clicked at " + e.getX()+ "," + e.getY());
}
Public void mouseEntered(MouseEvent e)
{
showStatus("Mouse has been Entered at " + e.getX()+ "," + e.getY());
// For loop:to make sure mouse entered is on status bar for a few sec
For (int i= 0; i<1000000; i++);
} 
Public void mouseExited (MouseEvent e)
{
showStatus ("Mouse has been Exited at " + e.getX()+ "," + e.getY());
}
Public void mousePressed(MouseEvent e)
{
showStatus ("Mouse pressed at " + e.getX()+ "," + e.getY());
}
Public void mouseReleased(MouseEvent e)
{
showStatus("Mouse released at " + e.getX()+ "," + e.getY());
}
Public void mouseDragged(MouseEvent e)
{
showStatus("Mouse dragged at " + e.getX()+ "," + e.getY());
}
Public void mouseMoved(MouseEvent e)
{
showStatus("Mouse moved at " + e.getX()+ "," + e.getY());
}
}
Since the applet implements MouseListener and MouseMotionListener it has to provide for the functionality of all their methods. Observe the mouseEntered() method has a small loop to ensure the mouse entered message remains on the status bar for a while. {mospagebreak title=Key Frienldy Codes} Keyboard Events and KeyListener: They are generated on pressing or releasing a key. The KeyEvent class contains constants to represent the keys pressed or typed. The event listener corresponding to these types of event is the KeyListener.

The following example puts forth key event handling. The applet contains a label and a text field. The recently typed in character in the text field is displayed in the status bar. Example 4.

// Key events and KeyListener.
/*
<applet code = "keyTest.class" width = 400 height = 400>
</applet>
*/
Import java.awt.*;
Import java.awt.event.*;
Import java.applet.Applet;
Public class keyTest extends Applet implements KeyListener
{
Public void init()
{
Label lab = new Label ("Enter Characters :");
add(lab);
TextField tf = new TextField(20);
add(tf);
tf.addKeyListener(this);
}
Public void keyPressed(KeyEvent e)
{}
Public void keyReleased(KeyEvent e)
{}
Public void keyTyped(KeyEvent e)
{
showStatus(" Recently typed characters are : " + e.getKeyChar());
}
}
Here the text field delegates its key events to the applet. The add() method adds the components Label and TextField to the applet.

 
 
>>> More Java & J2EE Articles          >>> More By Gayathri Gokul
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: