moshecristel
asked on
Capturing a KeyEvent with a JWindow
I'm having trouble getting a JWindow to capture a KeyEvent. I looked in the PAQ's and found this example:
import java.awt.event.*;
import javax.swing.*;
public class JWindowEvents extends JWindow {
public JWindowEvents() {
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {System.out.println("KeyPr essed");}
public void keyReleased(KeyEvent ke) {System.out.println("KeyRe leased");}
public void keyTyped(KeyEvent ke) { System.out.println("KeyTyp ed"); }
});
}
public static void main(String[] args) {
JWindowEvents jwe = new JWindowEvents(frame);
jwe.setSize(200, 200);
jwe.setLocation(200, 100);
jwe.setVisible(true);
jwe.requestFocus();
}
}
... I added the 'jwe.requestFocus()' but it still doesn't manage to capture the KeyEvents. I'm using the 1.4 SDK so I'm not sure if I should be going about it a different way now or not.
Any help would be great.
import java.awt.event.*;
import javax.swing.*;
public class JWindowEvents extends JWindow {
public JWindowEvents() {
addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent ke) {System.out.println("KeyPr
public void keyReleased(KeyEvent ke) {System.out.println("KeyRe
public void keyTyped(KeyEvent ke) { System.out.println("KeyTyp
});
}
public static void main(String[] args) {
JWindowEvents jwe = new JWindowEvents(frame);
jwe.setSize(200, 200);
jwe.setLocation(200, 100);
jwe.setVisible(true);
jwe.requestFocus();
}
}
... I added the 'jwe.requestFocus()' but it still doesn't manage to capture the KeyEvents. I'm using the 1.4 SDK so I'm not sure if I should be going about it a different way now or not.
Any help would be great.
I don't think a JWindow can actually recieve the focus so what you are trying to do will not work (as you have already discovered).
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
objects:
Bummer :( It's good to know, though. Thanks for the help.
Bummer :( It's good to know, though. Thanks for the help.
Happy I could help :-)
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com