Listening to OTHER keypress event while using JLIST

Hi Experts,
  I am using JLIST (javax.swing.*) to implement some functionality. My code listens to the UPARROW and DNARROW key automatically. Now I want to listen to other keys (like LEFTARROW and RTARROW) in my program to implement some other functionality (Like starting/stopping timer). I tried the following:

"frame.addKeyListener(translation)" where translation implements KeyListener. But since I'm using JLIST, I'm unable to listen to these keypress events.

Please help.
- Namit
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.

Assuming your list is called "jList1", this should work...


final String LEFT_ARROW = "left-arrow";
final String RIGHT_ARROW = "right-arrow";

jList1.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW ).put( KeyStroke.getKeyStroke( KeyEvent.VK_LEFT, 0), LEFT_ARROW );
jList1.getActionMap().put( LEFT_ARROW, new AbstractAction()
          public void actionPerformed(ActionEvent event)
            System.out.println( "Left pressed on list" ) ;
        } );
jList1.getInputMap( JComponent.WHEN_IN_FOCUSED_WINDOW ).put( KeyStroke.getKeyStroke( KeyEvent.VK_RIGHT, 0), RIGHT_ARROW );
jList1.getActionMap().put( RIGHT_ARROW, new AbstractAction()
          public void actionPerformed(ActionEvent event)
            System.out.println( "Right pressed on list" ) ;
        } );

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
namitkabraAuthor Commented:

Thanks a lot TimYates. You are a life saviour!!!

Initially it was'nt working. Then I changed the JComponent.WHEN_IN_FOCUSED_WINDOW to JComponent.WHEN_FOCUSED and it did the trick!!!

PS: Thanks experts-exchange
:-)  Glad I could help :-)
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

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.