AbstractAction as WindowListener

I am trying to separate a function from state in my GUI application using Action objects. I managed to use them to create menu items and buttons that have the same functionality.

My problem is this: I want to have the same action for the "exit" element in my menu and the close frame button.

Currently, I was able to solve this problem by adding the following WindowListener to the frame:

private class MainWindowListener extends WindowAdapter { @Override public void windowClosing(WindowEvent e) { new ExitAction(model).actionPerformed(new ActionEvent(e.getSource(), e.getID(), "Exit")); } } 

Is there a simpler and simpler way to do this?

+7
source share
1 answer

Event forwarding is convenient, but you can also use dispatchEvent() , as shown here .

Addendum: Additional examples that use Action are shown below.

  • LinePanel that connects buttons and keys.
  • ScrollAction that uses existing Swing actions.
  • KeyPadPanel that illustrates the forwarding action.
  • GraphPanel , which shows the actions of the graph editor on the toolbar.
+6
source

All Articles