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


Listening to OTHER keypress event while using JLIST

Posted on 2003-03-30
Medium Priority
Last Modified: 2008-02-01
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
Question by:namitkabra
  • 2
LVL 35

Accepted Solution

TimYates earned 700 total points
ID: 8235306
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" ) ;
        } );

Author Comment

ID: 8246851

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
LVL 35

Expert Comment

ID: 8248329
:-)  Glad I could help :-)

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

577 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