Why JWindow can not catch the KEY Type event?

J

JTL.zheng

my code is:

JWindow window= new JWindow();
window.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
System.out.println((int) e.getKeyChar());
}
});


but it did not print any thing when I type the keyboard
(however, It can catch the mouse click event)

What should I do if I want to catch the KEY type event in JWindow?
Thank you very much.
 
A

Andrew Thompson

JTL.zheng wrote:
..
JWindow window= new JWindow();

Somebody asked recently, what good
was a JWiindow, over an undecorated JFrame,
and apparently, *not* for..
window.addKeyListener(new KeyListener() {

..KeyListener! I expect it has something to do with
being keyboard focusable - put a text field in it, and
that may change it.

OTOH..
What should I do if I want to catch the KEY type event in JWindow?

What about something that 'looks just like it'?

<sscce>
import javax.swing.JFrame;
import java.awt.event.KeyListener;
import java.awt.event.KeyEvent;

public class TestFocus {

public static void main(String[] args) {

JFrame f = new JFrame();
f.setUndecorated(true);
f.addKeyListener(new KeyListener() {
public void keyTyped(KeyEvent e) {
System.out.println((int) e.getKeyChar());
}
public void keyReleased(KeyEvent e) {}
public void keyPressed(KeyEvent e) {}
});
System.out.println( "f.isFocusable() " + f.isFocusable() );
f.setSize(200,200);
f.setVisible(true);
}
}
</sscce>

HTH

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.aspx/java-general/200705/1
 
J

JTL.zheng

haha, Thank you very much, Andrew T
your "something that 'looks just like it'" is exactly what I want!
: )
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top