Java swing "children" windows
Asked Answered
S

4

7

Sorry for really simple question, but I wasnt able to find anything in the net, probably because I dont know the right terms to look for.

When you have a desktop application, there are many so called children windows: one for options, one for "about" and so on. How to make them in Java Swing (with NetBeans tools - optional)?

So far I just created another JFrame and on relevant event opened it the same way Main function launches, well, main JFrame. But there's something wrong with this method: when I close child window via x in the upper right corner, whole program terminates as if I was closing the main window. This is probably because NetBeans auto generated code for x and I can find and change it somewhere... but still I have a feeling that there must be a simpler proper way to add children JFrames (or JPanels or whatever is it for children windows) ;)

Stendhal answered 17/1, 2010 at 7:52 Comment(0)
D
10

... But there's something wrong with this method: when I close child window via x in the upper right corner, whole program terminates as if I was closing the main window. ...

Well, I would say you are on the right path. Just make sure that you do the following for your child frames :

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Then closing the child windows won't shut down your entire application.

HTH ! ;-)

Dunbar answered 17/1, 2010 at 10:19 Comment(0)
A
4

Do you mean a Dialog Window (see How to Make Dialogs) that you likely want to be modal to block user input to all other windows in the program (see An Overview of Dialogs)?

Adna answered 17/1, 2010 at 8:19 Comment(1)
Not exactly. I mean something like Options window. I.e. Firefox Preferences window. Unless it is called dialog window too.Stendhal
H
1

Go to the frame-design mode -> select your frame -> in the properties, the fist option. Select Dispose on close.

If you don't see the props: Ctrl + Shift + 7 or Window (in the frame menu) -> Properties

Hearken answered 17/1, 2010 at 12:52 Comment(0)
M
1

If you create frames, they aren't considered children windows. You can not set an owner for them as you could a JDialog. If you did want to create another JFrame, then you would have to set the close operation

frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

instead of

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  

The EXIT_ON_CLOSE operation will call System.exit(0); which will terminate the JVM. Just remember, that if your main JFrame is set to DISPOSE_ON_CLOSE then you must make sure that all your other frames have been properly disposed of, otherwise it won't exit until they have been.

Mortal answered 17/1, 2010 at 17:18 Comment(1)
Thank you all of you ;) Too bad I cannot formally accept more than one answer, all of them were helpful :)Stendhal

© 2022 - 2024 — McMap. All rights reserved.