Frage

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.

War es hilfreich?

Lösung 2

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.

Andere Tipps

The shorcuts work when the components are focused.I think focus is not there in your previously opened window.Try to get the focus back to the window where you need the shortcut to be worked fine.Try cliking on that window and apply the shortcut.Perhaps it will work.Otherwise you have to deal with get focus to the window

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top