I have four JFrames open in an application. I wish for one of them to be the base JFrame, and when one of the others is closed than everything except the base frame will dispose.

How do I link the other three JFrames so that when any one of them is closed (disposed) they all close?

Recommended Answers

All 3 Replies

Keep a List of the dependent frames in your base frame. When closing one of the dependents, call a method on the base to dispose of all dependents.

1) not possible, without huge code for AWT workaround

2) you are talking about JFrame#setDefaultCloseOperations(JFrame.EXIT_ON_CLOSE) with 3 JDialogs##setDefaultCloseOperations(JDilaog.DISPOSE_ON_CLOSE)

3) don't do it that this way its so hard job manage anything betweens four hight level container, you have to put there some hierarchy, result is JFrame as parent with three JDialogs which have got a parent

4) very carrefully with top-level containers, don't create lots of these Object, because they are still in the memory untill current JVM instance exists, you can only to remove its contents and dispose Graphics/Graphics2D

5) look at WindowListener, then you can create own class that manageing lifecycle for all of four Top-Level Containers separatelly or altogether, up to you,

Member Avatar

What I would do is...
One JFrame (your base frame) and 3 JDialogs.
I think that this would be easier to implement than 4 JFrames.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.