Java Technology Home Page
A-Z Index

Java Developer Connection(SM)
Online Training

Downloads, APIs, Documentation
Java Developer Connection
Tutorials, Tech Articles, Training
Online Support
Community Discussion
News & Events from Everywhere
Products from Everywhere
How Java Technology is Used Worldwide
Print Button
 

Help is available for each task.



    Task 1

    To receive the key events, add the applet itself as an KeyListener target. That is, the applet should implement this interface.


    To the class definition, add:

    implements KeyListener
    

    In the init() method, add:

    addKeyListener(this);
    
    This means that the current object is functioning as the listener, that is, the target, for all key events distributed by the source, in this case, the applet itself. Thus, this object, that is, the applet, must define the method(s) specified in the KeyListener interface.

    Task 2

    Define the keyPressed() method so that it calls increaseCount() to update the count instance variable and repaint() to display the current count in the applet window.


      public void keyPressed(KeyEvent e) {
        increaseCount();
        repaint();
      }
    

    Do not call paint() directly; instead, use repaint(), which indirectly calls paint(). paint() is already defined.

    By the way, the difference between the methods is that keyPressed() is called when a key is pressed (any key including modifiers such as Shift); keyReleased() is called when a key is released; and keyTyped() reports a higher level interpretation of keys. For example, if you press Shift, then "The A Key", and then release both, keyPressed() and keyReleased() will each be called twice. In addition, keyTyped() will be called once. Note that the KeyEvent object passed to keyTyped() reports character data, in this case 'A', whereas keyPressed() is typically used to process keycodes.

Copyright 1996-2000 jGuru.com. All Rights Reserved.


Print Button
[ This page was updated: 14-Jul-2000 ]
Products & APIs | Developer Connection | Docs & Training | Online Support
Community Discussion | Industry News | Solutions Marketplace | Case Studies
Glossary - Applets - Tutorial - Employment - Business & Licensing - Java Store - Java in the Real World
FAQ | Feedback | Map | A-Z Index
For more information on Java technology
and other software from Sun Microsystems, call:
(800) 786-7638
Outside the U.S. and Canada, dial your country's AT&T Direct Access Number first.
Sun Microsystems, Inc.
Copyright © 1995-2000 Sun Microsystems, Inc.
All Rights Reserved. Terms of Use. Privacy Policy.