wood burning stoves 2.0
The moose likes Swing / AWT / SWT and the fly likes mouse click to exit program? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "mouse click to exit program?" Watch "mouse click to exit program?" New topic

mouse click to exit program?

Zahariah Lloyd

Joined: Jan 01, 2013
Posts: 3
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

Joined: Jun 09, 2003
Posts: 4632
> 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:
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 46349
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

Joined: May 03, 2008
Posts: 5115

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.

luck, db
There are no new questions, but there may be new answers.
I agree. Here's the link: http://aspose.com/file-tools
subject: mouse click to exit program?
jQuery in Action, 3rd edition