Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

JWindow KeyListener

Thread view: 
Max - 29 Oct 2005 03:43 GMT
I have a JWindow inside a JFrame and I'm trying to read key input. I've
tried adding key listeners to both the JWindow and the JFrame, but
neither responds to my input. Any suggestions?
Chris Smith - 29 Oct 2005 04:35 GMT
> I have a JWindow inside a JFrame and I'm trying to read key input. I've
> tried adding key listeners to both the JWindow and the JFrame, but
> neither responds to my input. Any suggestions?

What do you mean?  You actually tried to add a JWindow as a component in
a JFrame?  That would cause an IllegalArgumentException.  So that's
probably not right.  Now, can you tell us what you really did?

Note that most of the time, adding a KeyListener to a JFrame is a bad
idea.  AWT events don't propogate through the component hierarchy, so
it's unlikely to have any effect.  Instead, you should look up the
methods in JComponent called getActionMap() and getInputMap(int), and
use them instead.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Max - 29 Oct 2005 04:44 GMT
myWindowOwner = new JFrame();
myWindow = new JWindow(myWindowOwner);

then later on

myWindowOwner.addKeyListener(this);
myWindow.addKeyListener(this);

None of those are able to detect key input. Mouse input works just fine
with mouse listener.
Chris Smith - 29 Oct 2005 05:05 GMT
> myWindowOwner = new JFrame();
> myWindow = new JWindow(myWindowOwner);
[quoted text clipped - 6 lines]
> None of those are able to detect key input. Mouse input works just fine
> with mouse listener.

Okay, back to the second part of my response, then.  Both JFrame and
JWindow contain a JRootPane, which contains such things as a content
pane, a glass pane, etc.  If one of those child components has focus
instead of the top-level window, then the KeyEvent will be lost.  
Instead, use getInputMap and getActionMap to associate your action with
a KeyStroke, and specify a more generic condition to getInputMap.

The API docs for getInputMap and getActionMap are in JComponent.

Signature

www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Max - 29 Oct 2005 05:24 GMT
Ah. Thanks for your help!


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.