javaswingkeylistenerjdialog

Why isn't the JDialog triggering the key listener's keyPressed method?


This is my code

 JToolBar customizeKeys = new JToolBar();
 customizeKeys.add(new ChangeKeyListen("left"));
 private class ChangeKeyListen extends AbstractAction{
    private JDialog myDialog;
    class KeyGetter extends KeyAdapter {
        @Override
        public void keyPressed(KeyEvent e) {
            super.keyPressed(e);
            OtherPanel.this.map(
                        KeyEvent.getKeyText(e.getKeyCode()),
                                            keyAction);
            myDialog.setVisible(false);
            myDialog.removeKeyListener(getKeyListeners()[0]);
        }
    };
    public ChangeKeyListen(String name) {
        super(name);
    }
    @Override
    public void actionPerformed(ActionEvent e) {
       myDialog = new JOptionPane().createDialog("Press a key");
       myDialog.setVisible(true);
       myDialog.requestFocusInWindow();
       System.out.println(myDialog.getFocusableWindowState());
       myDialog.addKeyListener(new KeyGetter());
       System.out.println( myDialog.getKeyListeners());
     }
}

What I am trying to do here is when the user clicks the JButton that was added to the JToolBar with the attributes of its action, the user will be prompted with my own customized dialog box. The user can then press any key to close the dialog box.(it actually just be invisible). When I run the application, everything looks fine. The JToolBar looks right and the button looks right. When I click the button, the correct controller behavior occurs as the dialog box is popped up.(just visible) However the key adapter's keyPressed method isn't being triggered at all when i press a key.

What I've done to debug this is to first make sure that the JDialog can first of all be focusable so it can receive key events from the keyboard. I did that with this line of

System.out.println(myDialog.getFocusableWindowState());

and what I got on the console was true. Next I made sure that the key listener was being set. I did that with

 System.out.println( myDialog.getKeyListeners());

and this printed out

[Ljava.awt.event.KeyListener;@350b914b

which I assumed was a correct memory address for an object allocated from the heap.

I then checked out similar threads.

My issue couldn't be Jbutton listener isn't triggered, why? because the dialog box showed up and I made sure that the key listener was added with the print key listeners line. I couldn't use what the user said in Trying to use key Listener because I need to listen for the key press and use that key press later in my program. And this doesn't help either Why wont this KeyEvent work? because I need a general reaction to key presses to obtain which key was pressed.

I know that keyPressed isn't being executed because I put a breakpoint inside the method and this print statement

   System.out.println(KeyEvent.getKeyText(e.getKeyCode()));  

wasn't printing anything on the console.

Does anyone know how i can fix this issue?


Solution

  • You are adding the KeyListener to the dialog created by the JOptionPane.

    However, focus is on the JButton on the dialog. KeyEvents are only dispatched to the component with focus so your key listener code is never invoked.

    Why are you trying to listen for any key to close the dialog? The is NOT user friendly. The user does not know that is the way to close the dialog since this is not a standard UI convention. The user should click on the button to close the dialog.

    If you really do need to listen to any key pressed while the dialog is open then check out Global Event Listeners which shows how you can use an AWTEventListener to listen for any key event regardless of which component has focus.