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  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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? 
Google.com  
JAVA

Event Handling In Java
By: Gayathri Gokul
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 146
    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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    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

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - New Object Initialization in Java
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek