1

I'm writing an application in which the user of the application switches between multiple windows or frames(in java).
Is there any other way to switch between these frames instead of making a frame visible to false/true?

2 Answers 2

4

Does it actually need to be different Frames (or JFrames) or is it just different sets of content? Do you need separate MenuBars, Frame Titles, etc? Generally speaking, an Application can have a single frame with multiple panels of content that switch in and out (Possibly using something like a CardLayout.) There are certainly ways to do what you want - are you saying you want to keep the different Frames visible while just bringing a different one to the front? If so, all Frames have a toFront() call that will pop it to the front of the display order, but you'll need to deal with focus issues as well.

Sign up to request clarification or add additional context in comments.

7 Comments

Your suggestion to use CardLayout is good....but didn't you think for that there will be multiple panels which might be initialized when the application start. And after that there will be a memory problem
It depends. I've created huge GUI's and rarely had memory issues unless there were huge JTables or TextAreas. Your other option is to re-create each panel/frame whenever needed, which can be a bit of overhead too. A few thousand GUI objects really don't take as much memory as you might think, provided you do it correctly.
One more thing I would like to know. Suppose there is a login form then when the user clicks on the LogIn button a new window to be opened, then what to do with the login form beside of setVisible(false)?
You can just remove the reference to it and it will be GC'd.
If there's no references being held to the frame or any of it's components (Make sure to remove any listeners) it should be removed automagically.
|
1

If window is not visible at the moment setVisible() is the right way. If window is invisible you can use requestFocus() and requestForcusInWindow().

4 Comments

I'm hoping for a solution which do not use setVisible() for switching between windows. Because by making a frame setVisible(false) will still be in memory and it could be a problem.
Do you just want to get rid of a Frame when it's closed? You can use a WindowListener for that.
@Kylar: Yes, it could be. But the user switches between the windows so the closed window again needs to be opened and will have some previous information.
@Mohammad Faisal, frame instance is just an ordinary object, so if you care about garbage collection of unneeded frames just care about references to it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.