JWindow KeyListener

M

Max

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?
 
C

Chris Smith

Max said:
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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
M

Max

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.
 
C

Chris Smith

Max said:
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.

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.

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

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top