views:

186

answers:

3

Hello everyone,

In Java, I am adding a KeyListener to a JWindow, but it is not getting any key events. If I used the same code but extend a JFrame instead, then everything works fine.

public class MyWindow extends JWindow {
    ...
    ...
    private void initComponents() {
        ...
        ...
        addKeyListener(new KeyListener() {
            public void keyPressed(KeyEvent e) {
                System.out.println("KEY PRESSED: " + e.getKeyCode());
            }

            public void keyReleased(KeyEvent e) {
                System.out.println("KEY RELEASED: " + e.getKeyCode());
            }

            public void keyTyped(KeyEvent e) {
                System.out.println("KEY TYPED: " +  e.getKeyCode());
            }

        });
    }
}

Anyone know how can I solve this by using a JWindow?

Please note that I am using Linux, so I am not sure if it is something to do with the platform.

Thanks

+1  A: 

You can try by adding the Listener to the content pane of your JWindow that you can obtain from window.getContentPane().addKeyListener(..) to see if it works.

The problem can be that if you have something focused that is inside the JWindow but it is not the JWindow itself events will be dispatched to inner focused item. You can also try by adding the KeyListener to whatever you've got inside that window..

EDIT: Searching a little bit on Sun's forum it seems that, to force accepting focus on a JWindow API you may do is

JWindow myWindow = ...
myWindow.setFocusableWindowState(true);
myWindow.setFocusable(true);

if it still doesn't work the best solution is to use a JFrame:

public class WindowTest {
  public static void main(String[] args) {
    JFrame frame = new JFrame();
    // remove menu bar and title bar
    frame.setUndecorated(true);
    frame.setVisible(true);
    JWindow window = new JWindow(frame); // this works
    window.setBounds(0, 50, 200, 200);
    window.setVisible(true);
  }
}

EDIT2: A complex solution may be to get the ActionMap with getActionMap() and then add an action for all keyboard keystrokes that just bufferizes it, then you can dispatch them whenever you want (after return key for example)

Jack
I already tried adding it using getContentPane(). As for the possibility that something has has the focus, I tried an empty JWindow and the problem is still there. Once I switch to a JFrame or JDialog without any further changes the listener seems to work.
Untitled
It doesn't work though. I have just tried in my Linux box
OscarRyz
@Support - multilanguage SO: I was surprised. Your question about extending `JWindow` remains.
trashgod
A: 

Looks like JWindow is pretty much broken in Linux.

The workaround is to use JFrame with setUndecorated( true )

http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5070056

OscarRyz
A: 

i also had exactly the same problem...,my solution was that in one class of my package i extended JFrame,on which key listener works perfectly,and in the paint method,i used the graphics instance of window{Graphics g=win.getGraphics();}...and i painted everything in the window n not on JFrame...before that i already made a call to gd{graphicsdevice}.setFullScreenWindow(win);

jaat