Sun Java Solaris Communities My SDN Account Join SDN
 
Code sample

Code Samples from the Java Developers Almanac 2000

 

Code Samples Index

These code examples and other materials are subject to Sun Microsystems, Inc. Legal Terms

Handling Events with an Anonymous Class

If an event handler is specific to a component (that is, not shared by other components), there is no need to declare a class to handle the event. The event handler can be implemented using an anonymous inner class. This example demonstrates an anonymous inner class to handle key events for a component.

 
 component.addKeyListener(new KeyAdapter() {
   public void keyPressed(KeyEvent evt) {
   }
 });

 

Examplets provided by permission of the publisher, Addision-Wesley, and Author Patrick Chan.
Order this book from Amazon