KeyListener Problem

Posted on 2004-11-17
Last Modified: 2012-05-05
I'm having a problem with a KeyListener, my keylistener is being added to a JFrame and when compiling it with sdk 1.4 it does not listen to the keyboard, with 1.3 works fine. It seems as the JFrame is not focused even though it is and even using the requestFocus() on the Jframe. Also, when compilin it with 1.3 it works ok but it does not receive my left arrow nor my right arrow key (not numeric pad).
What can I do ?
Question by:TheFred

    Author Comment

    I'm trying to add some buttons to my class, and i need to add some ActionListeners. but to do so i have to write implements ActionListener but to do that i have to erase implements KeyListener so how can i do both :S , this is my first time using listeners so please help
    LVL 2

    Expert Comment

    check this link it will give u complete idea
    LVL 17

    Accepted Solution


    1. You dont need to erase 'implements KeyListener'. Classes in Java can implement multiple interfaces, but they can extend just one. So you can do it like this:

    public class YourClass extends SomeOtherClass implements ActionListener, KeyListener

    Naturally, you have to implement all the methods defined in the interfaces that YourClass implements. Since you've now added ActionListener interface, you have to implement 'actionPerformed' method, like this:

    public void actionPerformed(ActionEvent e)
         /// here comes the code to execute when action is performed

    That should take care of your problem.

    2. Much cleaner solution: create seperate classes that implement ActionListener and KeyListener interface respectively. In your case that means two classes, like this:

       public class ActionHandler implements ActionListener
          public void actionPerformed(ActionEvent e)
       public class KeyHandler implements KeyListener
          public void keyPressed(KeyEvent e)

          public void keyReleased(KeyEvent e)

          public void keyTyped(KeyEvent e)

    And then work with those classes, like this:

         .addKeyListener(new KeyHandler());

         .addActionListner(new ActionHandler())

    LVL 7

    Expert Comment

    In general actionlisteners react to the control that has the listener attached to it and the event occurs within that object.  (pretty general statement).  Adding a keylistener to a frame may not always work if another control has the focus and consumes the event first.  There are many different ways to implement listeners and many are specific: anonymus, inner class, refrence same class, handling class, actions.
     basic examples:    look at Actions here, another way for handling events great for defining a common event used for Menus, buttons, toolbars, and so on.

    Take some time to go through the tutorial.

    Author Comment

    Is there any way i can avoid lossing the focus from my JFrame so that My keylistener allways work ??? Why am I having troubles when listening to the arrow keys? they rarely works , sometimes only the up and down :S

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    How to run any project with ease

    Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
    - Combine task lists, docs, spreadsheets, and chat in one
    - View and edit from mobile/offline
    - Cut down on emails

    Suggested Solutions

    Title # Comments Views Activity
    canBalance challenge 34 54
    seriesUp challenge 7 79
    copyEndy  challenge 15 37
    strDist challenge 35 68
    1. Package the applet into a JAR file. The applet must be in a JAR file before a certificate can be attached to it. Use the jar JDK utility. If the applet was previously referenced with the help of a codebase attribute in  tag, replace the codebase …
    For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
    Video by: Michael
    Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
    Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    9 Experts available now in Live!

    Get 1:1 Help Now