Exiting Java GUI application without using System.exit()

Discussion in 'Java' started by MS, Mar 4, 2005.

  1. MS

    MS Guest

    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
    MS, Mar 4, 2005
    #1
    1. Advertising

  2. MS

    Fahd Shariff Guest

    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);
    }
    });

    --
    Fahd Shariff
    http://www.fahdshariff.cjb.net
    "Let the code do the talking..."
    Fahd Shariff, Mar 4, 2005
    #2
    1. Advertising

  3. MS

    MS Guest

    Ok, thanks.

    Fahd Shariff emailed this:
    > 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);
    > }
    > });
    >
    > --
    > Fahd Shariff
    > http://www.fahdshariff.cjb.net
    > "Let the code do the talking..."
    >
    MS, Mar 4, 2005
    #3
  4. MS

    Rhino Guest

    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

    "MS" <matthews@mailsnare.---nojunktakeout---.net> wrote in message
    news:5%0Wd.28441$...
    > Ok, thanks.
    >
    > Fahd Shariff emailed this:
    > > 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);
    > > }
    > > });
    > >
    > > --
    > > Fahd Shariff
    > > http://www.fahdshariff.cjb.net
    > > "Let the code do the talking..."
    > >
    Rhino, Mar 4, 2005
    #4
  5. MS

    MS Guest

    Thanks, no threads to stop, useful info. though.

    MS

    Rhino emailed this:
    > 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
    >
    > "MS" <matthews@mailsnare.---nojunktakeout---.net> wrote in message
    > news:5%0Wd.28441$...
    >
    >>Ok, thanks.
    >>
    >>Fahd Shariff emailed this:
    >>
    >>>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);
    >>> }
    >>> });
    >>>
    >>>--
    >>>Fahd Shariff
    >>>http://www.fahdshariff.cjb.net
    >>>"Let the code do the talking..."
    >>>

    >
    >
    >
    MS, Mar 4, 2005
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Joe Smith
    Replies:
    4
    Views:
    65,827
    sandeep1976
    Nov 8, 2006
  2. Matthew Thorley
    Replies:
    2
    Views:
    299
    Matthew Thorley
    May 23, 2005
  3. Replies:
    5
    Views:
    417
    Steven Bethard
    Mar 3, 2006
  4. jacob navia
    Replies:
    3
    Views:
    550
    Nick Keighley
    Feb 24, 2010
  5. Keith Thompson
    Replies:
    10
    Views:
    684
    Tim Rentsch
    Mar 3, 2010
Loading...

Share This Page