1

Do I have to work with an ActionListener or AbstractAction?

EDITED BASED ON ANSWERS

So, the best way this is the best way to do it?

Action closeaction = new AbstractAction("Afsluiten"){

        @Override
        public void actionPerformed(ActionEvent ae) {
            System.exit(1);
        }

    };
    menuItem = new JMenuItem(closeaction);
Luc Peetersen
  • 133
  • 1
  • 9

1 Answers1

2

As @kleopatra comments, Action is the preferred abstraction, and AbstractAction is the right base class. In your handler, a non-zero status signifies an error condition. As an alternative, consider sending a WINDOW_CLOSING event, as shown here for JDialog.

Addendum: The WINDOW_CLOSING event is convenient if your application needs to take some action before terminating. Add a WindowListener to the example to see the effect:

this.addWindowListener(new WindowAdapter() {

    @Override
    public void windowClosing(WindowEvent e) {
        System.out.println(e);
    }
});
Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045