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

x
?
Solved

KeyListener Problem

Posted on 2004-11-17
6
Medium Priority
?
631 Views
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 ?
0
Comment
Question by:TheFred
5 Comments
 

Author Comment

by:TheFred
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
0
 
LVL 2

Expert Comment

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

http://leepoint.net/notes-java/30GUI/50keyboard/keydemo.html
0
 
LVL 17

Accepted Solution

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








0
 
LVL 7

Expert Comment

by:tomboshell
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.event/pkg.html
http://www.javaalmanac.com/egs/javax.swing.table/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.  
http://java.sun.com/docs/books/tutorial/uiswing/events/index.html
http://java.sun.com/docs/books/tutorial/uiswing/events/actionlistener.html
0
 

Author Comment

by:TheFred
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
0

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