Java KeyListener for JFrame is being unresponsive?

You must add your keyListener to every component that you need. Only the component with the focus will send these events. For instance, if you have only one TextBox in your JFrame, that TextBox has the focus.So you must add a KeyListener to this component as well The process is the same: myComponent.

AddKeyListener(new KeyListener ...) Note: Some components aren't focusable like JLabel For setting them to focusable you need to: myComponent. SetFocusable(true).

You must add your keyListener to every component that you need. Only the component with the focus will send these events. For instance, if you have only one TextBox in your JFrame, that TextBox has the focus.So you must add a KeyListener to this component as well.

The process is the same: myComponent. AddKeyListener(new KeyListener ...); Note: Some components aren't focusable like JLabel. For setting them to focusable you need to: myComponent.

SetFocusable(true).

Yea you were right, when the program starts you can slightly see that the focus is on the button A. Adding a keylistener to each button fixed this. Thats a little weird, I would think that adding a keylistener to the JFrame would work but I guess not.

Thanks! – Tomek Nov 13 '08 at 16:29.

If you don't want to register a listener on every component, you could add you own KeyEventDispatcher to the KeyboardFocusManager: public class MyFrame extends JFrame { private class MyDispatcher implements KeyEventDispatcher { @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent. KEY_PRESSED) { System.out. Println("tester"); } else if (e.getID() == KeyEvent.

KEY_RELEASED) { System.out. Println("2test2"); } else if (e.getID() == KeyEvent. KEY_TYPED) { System.out.

Println("3test3"); } return false; } } public MyFrame() { add(new JTextField()); System.out. Println("test"); KeyboardFocusManager manager = KeyboardFocusManager. GetCurrentKeyboardFocusManager(); manager.

AddKeyEventDispatcher(new MyDispatcher()); } public static void main(String args) { MyFrame f = new MyFrame(); f.pack(); f. SetVisible(true); } }.

1 Thanks. Simple solution works well. – Dan Howard Jul 11 '10 at 10:58 1 THANK YOU.

Been looking for this for a while. – ddopson Jun 11 at 3:48.

KeyListener is low level and applies only to a single component. Despite attempts to make it more usable JFrame creates a number of component components, the most obvious being the content pane. JComboBox UI is also often implemented in a similar manner.It's worth noting the mouse events work in a strange way slightly different to key events.

For details on what you should do, see my answer on Application wide keyboard shortcut - Java Swing.

The window focus should be at the window, of course but I don't think that's the problem. I expanded your code, tried to run it and it worked - the key presses resulted as print output. (run with Ubuntu through Eclipse): public class MyFrame extends JFrame { public MyFrame() { System.out.

Println("test"); addKeyListener(new KeyListener() { public void keyPressed(KeyEvent e) { System.out. Println("tester"); } public void keyReleased(KeyEvent e) { System.out. Println("2test2"); } public void keyTyped(KeyEvent e) { System.out.

Println("3test3"); } }); } public static void main(String args) { MyFrame f = new MyFrame(); f.pack(); f. SetVisible(true); } }.

I get all of the messages output also. Run in Windows command line. – Darrel Nov 13 '08 at 13:13 You get all the messages because in this example the JFrame has the focus.

Try adding a TextBox component to the JFrame and see what happens. – bruno conde Nov 13 '08 at 14:22.

Deion (and anyone else asking a similar question), you could use Peter's code above but instead of printing to standard output, you test for the key code PRESSED, RELEASED, or TYPED. @Override public boolean dispatchKeyEvent(KeyEvent e) { if (e.getID() == KeyEvent. KEY_PRESSED) { if(e.getKeyCode() == KeyEvent.

VK_F4){ dispose(); } } else if (e.getID() == KeyEvent. KEY_RELEASED) { if(e.getKeyCode() == KeyEvent. VK_F4){ dispose(); } } else if (e.getID() == KeyEvent.

KEY_TYPED) { if(e.getKeyCode() == KeyEvent. VK_F4){ dispose(); } } return false; }.

In order to capture key events of ALL text fields in a JFrame, one can employ a key event post processor. Here is a working example, after you add the obvious includes. Public class KeyListenerF1Demo extends JFrame implements KeyEventPostProcessor { public static final long serialVersionUID = 1L; public KeyListenerF1Demo() { setTitle(getClass().getName()); // Define two labels and two text fields all in a row.

SetLayout(new FlowLayout()); JLabel label1 = new JLabel("Text1"); label1. SetName("Label1"); add(label1); JTextField text1 = new JTextField(10); text1. SetName("Text1"); add(text1); JLabel label2 = new JLabel("Text2"); label2.

SetName("Label2"); add(label2); JTextField text2 = new JTextField(10); text2. SetName("Text2"); add(text2); // Register a key event post processor. KeyboardFocusManager.

GetCurrentKeyboardFocusManager() . AddKeyEventPostProcessor(this); } public static void main(String args) { JFrame f = new KeyListenerF1Demo(); f. SetName("MyFrame"); f.pack(); f.

SetVisible(true); } @Override public boolean postProcessKeyEvent(KeyEvent ke) { // Check for function key F1 pressed. If (ke.getID() == KeyEvent. KEY_PRESSED && ke.getKeyCode() == KeyEvent.

VK_F1) { // Get top level ancestor of focused element. Component c = ke.getComponent(); while (null! = c.getParent()) c = c.getParent(); // Output some help.System.out.

Println("Help for " + c.getName() + ". " + ke.getComponent().getName()); // Tell keyboard focus manager that event has been fully handled. Return true; } // Let keyboard focus manager handle the event further.

Return false; } }.

For a working example you might consider adding the imports. I usually add 'package imports' to keep them short. Otherwise, +1.

Interesting technique. – Andrew Thompson Jul 16 at 4:36.

I got the same problem until I read that the real problem is about FOCUS the your JFrame has already added Listeners but tour frame is never on Focus because you got a lot of components inside your JFrame that also are focusable so try: JFrame. SetFocusable(true); Good Luck.

I have been having the same problem. I followed Bruno's advice to you and found that adding a KeyListener just to the "first" button in the JFrame (ie, on the top left) did the trick. But I agree with you it is kind of an unsettling solution.So I fiddled around and discovered a neater way to fix it.

Just add the line myChildOfJFrame. RequestFocusInWindow(); to your main method, after you've created your instance of your subclass of JFrame and set it visible.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions