Link to home
Start Free TrialLog in
Avatar of moshecristel
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("KeyPressed");}
     public void keyReleased(KeyEvent ke) {System.out.println("KeyReleased");}
     public void keyTyped(KeyEvent ke) { System.out.println("KeyTyped"); }
   });
 }
 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.
Avatar of Mick Barry
Mick Barry
Flag of Australia image

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
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of moshecristel
moshecristel

ASKER

objects:

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