Starting KeyEventDispacther the first time JDialog starts and removes it

I am making an application that needs to read some characters from the keyboard and interpret them.

To capture the keys, I open JDialog and set the KeyEventDispatcher, so I can capture the characters in the dispatchKeyEvent method. JDialog has a button that removes KeyEventDispatcher and removes JDialog.

There are two problems: - The first time JDialog opens, it does not look like KeyEventDispatcher. - When I close and open this JDialog, KeyEventDispatchers accumulate (open it the first time, it doesn’t work there, open the second time, one works there, open the third time, 2 works there, ...)

It seems that KeyEventDispacthers are installed when the JDialog closes and is not removed, and is not installed when the JDialog is opened and removed when it closes.

Someneone could help me understand what is happening and how can I fix it?

Here's a simplified version of the JDialog class (only with 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 is another way to capture keys, I will be happy to see him. So far I have tried:

  • KeyListener didn't work even when I added it to JButton and contentPane
  • KeyEventPostProcessor, had the same effect as when using KeyEventDispatcher
  • KeyBinding did not work and does not seem to be the best choice, since I have to grab everything printed
+4
source share
1 answer

Unable to play back first.

Can play stacking: the dispatcher is not deleted when the dialog box is closed by clicking on the close icon in the header. In this case, strokes printed in the main frame are printed after closing the dialog.

The dispatcher can be safely removed this way both in dispose and in the WindowListener (and not 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); 
+4
source

Source: https://habr.com/ru/post/1392322/


All Articles