Solved

Capturing a KeyEvent with a JWindow

Posted on 2002-07-21
4
289 Views
Last Modified: 2008-02-01
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.
0
Comment
Question by:moshecristel
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7168436
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).
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7168438
0
 

Author Comment

by:moshecristel
ID: 7168542
objects:

Bummer :(  It's good to know, though.  Thanks for the help.
0
 
LVL 92

Expert Comment

by:objects
ID: 7170556
Happy I could help :-)

http://www.objects.com.au/staff/mick
Brainbench MVP for Java 1
http://www.brainbench.com
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now