where do the keystrokes go?

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);
}
}
 
C

Chris Smith

Martijn Mulder said:
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.

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

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

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

Martijn Mulder

:) 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);
}
}
 

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,777
Messages
2,569,604
Members
45,216
Latest member
topweb3twitterchannels

Latest Threads

Top