Java
  Home arrow Java arrow Page 6 - Event Handling In Java
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Event Handling In Java
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 118
    2003-09-11

    Table of Contents:
  • Event Handling In Java
  • Going Into Over Drive
  • Relax Back Event Handling In A Nut Shell
  • Event-Handling In Java
  • Amazingly Simple Applets
  • Naughty Examples For Those Little Mice
  • A Quick Recap

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Event Handling In Java - Naughty Examples For Those Little Mice


    (Page 6 of 7 )

    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 Articles
    More By Gayathri Gokul


     

       

    JAVA ARTICLES

    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming
    - Gaming Development Setup





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway