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);
As @kleopatra comments,
Actionis the preferred abstraction, andAbstractActionis the right base class. In your handler, a non-zero status signifies an error condition. As an alternative, consider sending aWINDOW_CLOSINGevent, as shown here forJDialog.Addendum: The
WINDOW_CLOSINGevent is convenient if your application needs to take some action before terminating. Add aWindowListenerto the example to see the effect: