Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

mouse click to exit program?

 
Zahariah Lloyd
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is probably an extremely noob question, but is there a mouse event that I can code into my program in order that if you click the button, it will exit the program??
Say if I had this code to complete:

What would I put here please to make the button click close the frame and stop the program from running??
Any advise, or directions will be greatly appreciated and thanks in advance!!
 
Michael Dunn
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
> but is there a mouse event...it will exit the program

you're already using an actionEvent, just replace this line
// TODO add your handling code here:
with
System.exit(0);
 
Campbell Ritchie
Sheriff
Pie
Posts: 48954
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the ranch
Yes, System.exit(0); will successfully exit your program, but it is a bit vicious. What if you are threading, and the other thread is updating a file with your results? If you exit now, the other thread will be terminated too, maybe resulting in a corrupted file. Write an exit() method which terminates your application tidily and call that from the action performed method.
Alternatively: use the old AWT method of writing a listener which has a windowClosed method in. It might be a WindowListener and you can probably make your frame implement it.
 
Darryl Burke
Bartender
Posts: 5126
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Zahariah Lloyd wrote:What would I put here please to make the button click close the frame and stop the program from running??

dispose() will close the frame. What happens next depends on what you have set as the frame's default close operation, and any WindowListener#windowClosing/windowClosed logic.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic