Search code examples
javaswingkeyboard-eventsjdialogkeyevent

Running KeyEventDispacther the first time JDialog opens and removing it afterwards


I'm doing an application that must read some characters via keyboard and interpret them.

To capture the keys, I open a JDialog and set a KeyEventDispatcher, so I can capture the characters in the dispatchKeyEvent method. In the JDialog there's a button that removes the KeyEventDispatcher and disposes of the JDialog.

There are two problems with it:
- The first time the JDialog is opened, it's like the KeyEventDispatcher was not set
- As I close and open this JDialog, the KeyEventDispatchers are cumulating (open he 1st time, there's none running; open the 2nd time, there's one running, open the 3rd time, there're 2 running, ...)

It seems that the KeyEventDispacthers are being set when the JDialog is closed and not removed, instead of being set when the JDialog is opened and removed when it closes.

Someneone could please help me understand what's happening and how I can fix it?

Here's a simplified version of the JDialog class (with only the key capture part):

public class SequenceDialog {
    private JDialog dialog;
    private JButton finishButton;
    private DialogKeyEventDispatcher keyEventDispatcher;

    public SequenceDialog() {
        initializeDialog();
    }

    private void initializeDialog() {
        dialog = new JDialog();
        finishButton = new JButton("Finish");

        finishButton.addActionListener(new FinishButtonListener());
        dialog.setModalityType(ModalityType.APPLICATION_MODAL);
        dialog.add(finishButton);
        setKeyListener();
        dialog.setVisible(true);
        dialog.pack();
    }

    /** Adds the KeyEventDispacther */
    private void setKeyListener() {
        keyEventDispatcher = new DialogKeyEventDispatcher();

        KeyboardFocusManager manager = KeyboardFocusManager
                .getCurrentKeyboardFocusManager();
            manager.addKeyEventDispatcher(keyEventDispatcher);
    }

    private class FinishButtonListener implements ActionListener {
        public void actionPerformed(ActionEvent e) {
            //Removing the KeyEventDispacther
            KeyboardFocusManager manager = KeyboardFocusManager
                    .getCurrentKeyboardFocusManager();
                manager.removeKeyEventDispatcher(keyEventDispatcher);

            dialog.dispose();
        }
    }

    /** The KeyEventDispatcher to be executed */
    private class DialogKeyEventDispatcher implements KeyEventDispatcher {
        public boolean dispatchKeyEvent(KeyEvent e) {
            if(e.getID() == KeyEvent.KEY_PRESSED) {
                System.out.println(KeyEvent.getKeyText(e.getKeyCode()));
            }

            return false;
        }
    }
}

If there's another way of capturing the keys, I will gladly see it. So far, what I have tried:

  • KeyListener, didn't work even when I added it to the JButton and the contentPane
  • KeyEventPostProcessor, had the same effect as using KeyEventDispatcher
  • KeyBinding, didn't work and doesn't seem like the best choice, as I must capture everything typed

Solution

  • Can't reproduce the not-working on first.

    Can reproduce the stacking: the dispatcher is not removed when closing the dialog by clicking on the close icon in the title. In this case, strokes typed in the main frame are printed after the dialog has been closed.

    The dispatcher can be reliably removed by so in both dispose and a WindowListener (instead of in the finish action):

        private void initializeDialog() {
            dialog = new JDialog() {
    
                @Override
                public void dispose() {
                    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                    manager.removeKeyEventDispatcher(keyEventDispatcher);
                    LOG.info("disposed: " + manager);
                    super.dispose();
                }
    
            };
            WindowListener l = new WindowAdapter() {
    
                @Override
                public void windowClosing(WindowEvent e) {
                    KeyboardFocusManager manager = KeyboardFocusManager.getCurrentKeyboardFocusManager();
                    manager.removeKeyEventDispatcher(keyEventDispatcher);
                    LOG.info("closing: " + manager);
                }
    
            };
            dialog.addWindowListener(l);