I'm a bit new to java programming and I wanted to make my application close when I click on a JMenuItem (named Exit). The code behind it is partly generated with NetBeans GUI builder.
private void CloseWindow(java.awt.event.MouseEvent evt) {
// TODO add your handling code here:
System.exit(0);
}
But for some reason, the app won't close when I click the menu item.
04-09-2009, 08:13 AM
Armaron
Doesn't anybody know why my exit button doesn't work?
EDIT - Ok found it. For some reason I can use a JMenu instance to make events occur, but a JMenuItem event doesn't do anything. Can anybody explain that to me?
04-13-2009, 03:50 AM
Razee Marikar
I think you have to use addActionListener() in the menuItem. A menu item may not only be fired through mouse clicks, people may also use the keyboard to use a menu, or a menu 'hot key' to directly acces the menu item. So, to provide maximum usability, you should always use the actionListener for default actions, like selecting a menu item, clicking on a button / checkbox, etc.