Exiting Java GUI application without using System.exit()

M

MS

Hi,

What is the correct and safe way of exiting a Java JFrame GUI application?

My GUI has an Exit button, but while searching on this, I discovered that
System.exit(0) is not a good way to terminate the application. How should
it be done.

Thanks,

MS
 
F

Fahd Shariff

You need to explicitly call System.exit(exit_value) to exit a Swing
application.

You might however wish to perform some cleanup e.g. closing file
handles, database connections, deleting temporary files etc before
exiting.

exitButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
cleanUp();
System.exit(0);
}
});
 
R

Rhino

One additional point which Fahd didn't mention explicitly.

Someone told me the other day that the main objection to System.exit() is
that it will terminate other threads that are running in the application. I
was told that you should make a serious effort to stop any such threads
cleanly before attempting to do the System.exit().

Rhino
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top