?
Solved

Listening to OTHER keypress event while using JLIST

Posted on 2003-03-30
3
Medium Priority
?
390 Views
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
0
Comment
Question by:namitkabra
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

Accepted Solution

by:
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" ) ;
          }
        } );
0
 

Author Comment

by:namitkabra
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
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8248329
:-)  Glad I could help :-)
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying 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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month12 days, 17 hours left to enroll

777 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