Search code examples
javadialogkeystroke

InputMap/ActionMap does not work after opened SubDialog


I have tried to make a Dialog Close when I press ESC and accept when I press ENTER. So I have build a superclass "CloseableDialog" using Input/ActionMap like this:

    getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ENTER, 0), "accept");
    getRootPane().getActionMap().put("accept", acceptAction);

    getRootPane().getInputMap().put(KeyStroke.getKeyStroke(KeyEvent.VK_ESCAPE, 0), "cancel");
    getRootPane().getActionMap().put("cancel", cancelAction);

Now I built several Dialogs extending CloseableDialog. It works as aspected at first time (closing when pressing ESC, accepting when press ENTER), but when I opened a SubDialog (Dialog in Dialog) only the Subdialog was closeable by pressing ESC (ESC -> SubDialog closes, other Dialog Visible -> ESC-> nothin happens).

Any ideas?

I think, the diposed SubDialog gets still the inputs of the other Dialog, but I am not sure.


Solution

  • I've got it!

    Reading http://docs.oracle.com/javase/tutorial/uiswing/misc/keybinding.html told me, that there are three InputMaps:

    • JComponent.WHEN_FOCUSED
    • JComponent.WHEN_ANCESTOR_OF_FOCUSED_COMPONENT
    • JComponent.WHEN_IN_FOCUSED_WINDOW

    By calling getRootPane.getInputMap() I got the "default" WHEN_FOCUSED Inputmap, but by calling a Subdialog the rootPane seems to lost the focus and nothing helped to get the Focus back.

    Now I am using getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(...) and it works.

    I am a little bit amazed, that it has worked before until opening a Subdialog.