• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Extending question

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have created a class that extends the KeyEvent class so I could add a bunch of extra fields. It compiled cleanly. To use a KeyListener interface I must pass a KeyEvent object as a parameter to the keyPressed(), keyReleased() and keyTyped() methods. How do I now gain access to my child class within the Listener?
I've ruled out casting because it's a child.
Thanks for any guidance.
Paul
 
Ranch Hand
Posts: 241
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Paul.
It sounds like the approach you've tried follows these lines:

(Don't let the inner class intimidate you, my putting it here arises out of convenience and not necessity.)
The problem is, the class's implementing of java.awt.event.KeyListener requires you to have a method with the signature public void keyPressed( KeyEvent ke ), and it doesn't like public void keyPressed( MyKeyEvent mke ).
If you try to compile this code, you'll get

But if you add public void keyPressed( KeyEvent ke ), your code will never get called.
The solution, I think, Paul, is not to extend KeyEvent, but rather to "wrap" a bona fide KeyEvent inside a class of your own design. This strategem is a design pattern, called the "Decorator" or "Wrapper" design pattern.

In this case, instead of overriding or adding functionality in a subclass that you create, you make KeyEvent an instance variable to your class, and add functionality to your class by "decorating" that instance variable.
I hope that makes sense; all my little toy example does here is print to standard output the key you press while the frame is active.
Good luck, Paul.
Art
[This message has been edited by Art Metzer (edited May 25, 2001).]
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This does make sense and is extremely helpful. Thanks!!
Paul
 
Paul Keohan
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unfortunately this didn't work. I still don't have access to the new final variables I set in MyKeyEvent. Wherever the KeyEvent object originally comes from only understands the codes that are in that class. I'm trying to get the listener to understand a new final variable that I have set. It looks like I need the KevEvent object that's passed in keyPressed() to have already 'seen' MyKeyEvent.
Is this possible?
Thanks.
Paul
 
reply
    Bookmark Topic Watch Topic
  • New Topic