KeyListener Problem

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 ?
TheFredAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

TheFredAuthor Commented:
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
jprgnCommented:
check this link it will give u complete idea

http://leepoint.net/notes-java/30GUI/50keyboard/keydemo.html
0
Dejan PažinHead of SW DevelopmentCommented:

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
tomboshellCommented:
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
TheFredAuthor Commented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.