Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


KeyListener Problem

Posted on 2004-11-17
Medium Priority
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

ID: 12611463
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

Expert Comment

ID: 12612194
check this link it will give u complete idea

LVL 17

Accepted Solution

Dejan Pažin earned 270 total points
ID: 12612598

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())


Expert Comment

ID: 12612621
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: http://www.javaalmanac.com/egs/java.awt.event/pkg.html
http://www.javaalmanac.com/egs/javax.swing/pkg.html    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

ID: 12616567
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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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:
Suggested Courses
Course of the Month14 days, 16 hours left to enroll

578 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