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
posted
0
> 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
Joined: Oct 13, 2005
Posts: 32611
4
posted
0
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.
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.