1

In my program it opens a window if an action is happened. After I have filled out information in this window, and entered a button, the window dispose().

The window is picked up in a program outside my main program, but when I close this window, my main program stops. How can I prevent this from happening?

Thanks for your help!

user3176870
  • 31
  • 1
  • 10

4 Answers4

4
  1. You can set the defalaultCloseOperation property of the second frame to DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE

  2. Don't even use two frames. Use a modal JDialog instead for a secondary frame. See more at How to Use Dialogs. Read more about Modality. And for a good read, see The Use of Multiple JFrames, Good/Bad Practice?

  3. Forget about number 1. and go straight to 2.

Community
  • 1
  • 1
Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
3

If using JFrame or extending it you can use setDefaultCloseOperation() method like:

frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
// or
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
Abdul Fatir
  • 6,159
  • 5
  • 31
  • 58
0

The dispose command is from the AWT Bundle, and this may cause problems, as you are attempting to "close" a swing window with an AWT command.

You can close the window with this command:

windowName.setVisable(false);

windowName is the name of the object representing the window. If you are extending a class, and have no object, you can use this

More Information on the Dispose Method: "In general java.awt.Window.dispose() is used on a GUI component that is a subclass of Window, in order for that specific GUI Window object (and its children) to properly release and destroy native UI resources (such as the screen). Garbage collection (and the eventual exiting of the entire VM) may happen as a result of dispose(), but is not directly connected with the dispose() call itself." From: https://www.java.net/node/684412

Jake Chasan
  • 6,290
  • 9
  • 44
  • 90
  • but after i have closed the window by either pressing close button(X) or one of the three buttons in the window, i have typed in the command dispose(). I want to dispose the window, but to continue the main program. – user3176870 May 19 '14 at 13:54
  • Why is that necessary? – Jake Chasan May 19 '14 at 13:55
  • Java's Garbage Collection should automatically dispose of the lightweight Swing components after they are not in use. – Jake Chasan May 19 '14 at 13:56
  • because after the operator of the program have typed in the data, the window is not necessary any more. – user3176870 May 19 '14 at 13:57
  • So once the window closes, is there a problem with letting the Java VM's Automatic Reference Counting (Garbage Collection) take care of managing the disposal? – Jake Chasan May 19 '14 at 13:58
0
windowName.setVisable(false);

doesn't seems to be a good choice. What if user want to exit the program?

check this question - How to hide a JFrame in system tray of taskbar

Community
  • 1
  • 1
Mohammad Faisal
  • 5,783
  • 15
  • 70
  • 117