javaswingjframejdialogjmenuitem

JDialog is interfering with JFrame owner MenuItem accelerators


I have a JDialog (non modal) with a JFrame owner. The problem I have is the that when the JDialog has the focus it is preventing the ActionListener associated with the JFrame JMenuItem accelerators getting called.

I have tried to implement a KeyEventDispatcher in the JDialog and redispatch the event to the JFrame owner;

DefaultKeyboardFocusManager.getCurrentKeyboardFocusManager().redispatchEvent(owner, e);

This did not work, as it did not trigger a call to the JMenuItem accelerator ActionListener.

Does anyone have a work around for this?

Cheers


Solution

  • Thank you @abra.

    I was able to find a work around for this issue.

    By using the KeyEvent argument of the dispatchKeyEvent() method from the KeyEventDispatcher interface I was able to determine the associated KeyStroke used to generate the KeyEvent. This enabled me to find the JMenuItem (if any) in the application's main JFrame with an associated KeyStroke accelerator. Once I have the JMenuItem associated with the KeyStroke, it can be programmatically activated by the JMenuItem.doClick() method.

    This effectively redispatched the KeyEvent from the child JDialog to the application's main JFrame.

    I suppose KeyBindings could have solved this issue but as this is legacy code, reworking the Menu structure of the application main JFrame would have been a less optimal solution.