> I try to attach a KeyListener to my JComponent but no
> keystrokes make it to the JComponent. MouseMotions, on
[quoted text clipped - 5 lines]
> stdout. Pressing keys when the JFrame has focus yields
> no output.
Well, for one thing, your component needs focus. The containing frame
having focus isn't good enough. (Can you imagine if every time you
pressed a key while a dialog had focus, EVERY textfield in that dialog
received the key event?) To give focus to your new control, you'll
first need to call setFocusable(true).

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Martijn Mulder - 18 Dec 2005 19:54 GMT
:-) Thank you, Chris. Hours of thumbing through books
led nowhere, posting did. Here is the fixed code. Moving
the mouse over the JFrame or pressing keys when JFrame
has focus makes the JComponent respond:
//class MouseAndKeys
class MouseAndKeys extends javax.swing.JComponent
{
//constructor
MouseAndKeys()
{
super();
setFocusable(true);
addMouseMotionListener(new javax.swing.event.MouseInputAdapter()
{
public void mouseMoved(java.awt.event.MouseEvent a)
{
System.out.println("mouseMoved()");
}
});
addKeyListener(new KeyAdapter()
{
public void keyPressed(java.awt.event.KeyEvent a)
{
System.out.println("keyPressed()");
}
});
}
//main
public static void main(String[]a)
{
javax.swing.JFrame jframe=new javax.swing.JFrame("MouseAndKeys");
jframe.getContentPane().add(new MouseAndKeys());
jframe.setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
jframe.pack();
jframe.setSize(610,377);
jframe.setVisible(true);
}
}