M
Martijn Mulder
I try to attach a KeyListener to my JComponent but no
keystrokes make it to the JComponent. MouseMotions, on
the other hand, are percieved by the JComponent. I do not
understand where the keystrokes go, since both Listeneres
are attached in identical ways. This code illustrates the
problem. When the mouse is moved over the JFrame,
a long stream of 'mouseMoved()' messages is send to
stdout. Pressing keys when the JFrame has focus yields
no output.
//class MouseAndKeys
class MouseAndKeys extends javax.swing.JComponent
{
//constructor
MouseAndKeys()
{
super();
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);
}
}
keystrokes make it to the JComponent. MouseMotions, on
the other hand, are percieved by the JComponent. I do not
understand where the keystrokes go, since both Listeneres
are attached in identical ways. This code illustrates the
problem. When the mouse is moved over the JFrame,
a long stream of 'mouseMoved()' messages is send to
stdout. Pressing keys when the JFrame has focus yields
no output.
//class MouseAndKeys
class MouseAndKeys extends javax.swing.JComponent
{
//constructor
MouseAndKeys()
{
super();
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);
}
}