Unable to implement KeyListener

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Peter Nicholson
    New Member
    • Apr 2011
    • 2

    Unable to implement KeyListener

    Hi all,

    firstly please advise if I've posted incorrectly as this is my first post.

    Consider the following files :

    Grandparent - public class

    Parent - public abstract class extends Grandparent

    Child - public abstract class extends Parent


    I cannot change the class type or extensions in these files as these are a structure I did not create.

    My issue is I am trying to create a KeyListener in a subclass of Child - I'll call Egg:

    import java.awt.event. KeyListener

    Egg public class extends Child implements KeyListener

    I have tried various ways of achieving this:

    - addKeyListener( keyListener)

    - KeyListener listener = new KeyListener()

    but end up with a "symbol not found ....KeyListener " when compiling.

    I can create a working key listener in it's own separate file, I just can't seem to get it into this structure.

    Any suggestions ?

    And as I said please point out if I am posting incorrectly.

    Thank you.
  • sanat
    New Member
    • Apr 2011
    • 5

    #2
    as Egg implements the KeyListener it can handle the KeyEvents.

    So the following code should do the task:

    addKeyListener( new Egg());

    NOTE : if the component to which you want to make key event sensitive is there in the Egg class then write :
    addKeyListener( this);

    Pls ask if u ve any doubt......

    Comment

    • Peter Nicholson
      New Member
      • Apr 2011
      • 2

      #3
      Thanks sanat this cleared it up for me, all working now.

      Comment

      Working...