Win a copy of Cross-Platform Desktop Applications: Using Node, Electron, and NW.js this week in the JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

JMenu – exit application problem!  RSS feed

 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ok javaranch here’s my problem!
I’m trying to add a JMenu to my excising application, and the one function I want it to perform (at the moment) is to have a option that exits the application (as in it cleanly shuts it down)! I though the flowing code would work

However I get this compile error on the third line;
Type mismatch: cannot convert from void to Action
Im guessing that means I can’t use a System command on Action type!
If thats true is there an obvious way to put an System.exit(0); (or equivalent command) on an a JMenu
Thanks in advance
sam
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Samuel Woodhams wrote:

You already have the declaration so you're halfway there
After you've read this I'm sure you will understand how to do it. Combine that with an anonymous inner class and you basically have your own code with 6 lines of code extra:
- 2 for the brackets for the anonymous inner class
- 1 for the actionPerformed method declaration
- 2 for the brackets for the actionPerformed method
- 1 for your call to System.exit(0);

If you also put in the name in your constructor, you can even remove this line:
 
Samuel Woodhams
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much Rob yet again JavaRanch comes to the rescue

The code looks like this and it works

again thanks very much
sam
 
Rob Spoor
Sheriff
Posts: 21044
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Rob Camick
Ranch Hand
Posts: 2787
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I know you have a working answer, but Closing An Application gives a slightly different version of an ExitApplication that you can use.

Instead of just exiting the application is generate a windowClosing event. The writeup explains why this might be usefull.

It also sets up a standard accelerator key to exit the application.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!