setDefaultCloseOperation()

Classic programmed-learning exercises, refreshed for modern Java and presented in the current MrStyner.com portfolio style.

Modern Java noteThis archive has been refreshed for Java 25 LTS. Core language concepts remain useful, while outdated setup instructions and browser-era Java are labeled or replaced. Java 26 is the current feature release; Java 25 is used here as the stable teaching baseline.
go to previous page   go to home page   go to next page

Answer:

No. In a complex application with many frames, you probably want only to close the frame who's button you clicked. You probably want to keep the application running.

setDefaultCloseOperation()

The setDefaultCloseOperation() method is used to specify one of several options for the close button. Use one of the following constants to specify your choice:

  • JFrame.EXIT_ON_CLOSE — Exit the application.
  • JFrame.HIDE_ON_CLOSE — Hide the frame, but keep the application running.
  • JFrame.DISPOSE_ON_CLOSE — Dispose of the frame object, but keep the application running.
  • JFrame.DO_NOTHING_ON_CLOSE — Ignore the click.

If you forget to call setDefaultCloseOperation() you will get JFrame.HIDE_ON_CLOSE by default. This can be frustrating, because it looks like you have "killed" the program, but it keeps on running, and you see no frame.

QUESTION 9:

Could different frames pick different close operations?