This week's book giveaway is in the HTML Pages with CSS and JavaScript forum.
We're giving away four copies of Testing JavaScript Applications and have Lucas da Costa on-line!
See this thread for details.
Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Extending question

 
Ranch Hand
Posts: 411
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
 
Just the other day, I was thinking ... about this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic