Solved

catch child compent's key events from container?

Posted on 2002-06-13
4
222 Views
Last Modified: 2010-03-31
Hi,
   I have a JPanel which contain some image JLabels.
How can I add a key listener to JPanel that catch
key events even JLabels
is in focus.

Thanks.
Sean
0
Comment
Question by:xzhang4
[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
4 Comments
 
LVL 19

Expert Comment

by:Jim Cakalic
ID: 7076115
According to the javadoc, JLabel cannot get the keyboard focus. Do you have some sample code that doesn't work as you had expected?

Jim
0
 

Author Comment

by:xzhang4
ID: 7076307
OK.  I am wrong.   How about JPanel contain some JButtons.
Thanks.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7076781
Try getting the input map as follows:

InputMap map = panel.getInputMap(JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT);

Add add your input action to this map.
0
 
LVL 92

Expert Comment

by:objects
ID: 7082828
0

Featured Post

Enroll in June's Course of the Month

June's Course of the Month is now available! Every 10 seconds, a consumer gets hit with ransomware. Refresh your knowledge of ransomware best practices by enrolling in this month's complimentary course for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

734 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