How to free JFrame resources? - java

I have a question for you:
Calling setVisible(false) on a JFrame, makes it disapper, right?
Calling again setVisible(true) makes it appear again, this means that the object is completely still in RAM, right?
If I need to get rid of everything right after I press the "Close" button, what's the correct way (this JFrame is displayed when I press a "config" button in the main UI, so it should disappears (also from RAM) when I press the close button)?
Thanks for your time and sorry for my bad English

playing with the visibility of the JFrame will not allow the OS to reclaim its native resources allocated by the window, a way for doing so is to call JFrame#dispose
/**
* Releases all of the native screen resources used by this
* {#code Window}, its subcomponents, and all of its owned
* children. That is, the resources for these {#code Component}s
* will be destroyed, any memory they consume will be returned to the
* OS, and they will be marked as undisplayable.
* ...
*/
public void dispose() {
doDispose();
}
another way to invoke dispose on closing the JFrame is to do the following
frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);

You have to set default close operation of the frame.
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
See this link
You can also try to clean up after your frame has been closed/disposed.
Find the relevant documentation here.

Related

When closing the JFrame main method execution should not be stopped [duplicate]

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!
You can set the defalaultCloseOperation property of the second frame to DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE
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?
Forget about number 1. and go straight to 2.
If using JFrame or extending it you can use setDefaultCloseOperation() method like:
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
// or
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
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
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

Swing, Java, How to close() anything jFrame?

There is a changePassword Frame.
Since the window for each user is different, password change uses one frame. (changePW jFrame)
Because the password change may be canceled or it may be wrong, the parent window that opened the password change window did not disappear until the change was completely completed. So I did not dispose ().
The problem is,
If the login () -> userX (another frame by the logged in person) -> changePW () is executed sequentially, the password change is normally successful.
How can I dispose () all of the parent frames with the corresponding changePW () after successfully changing the password? (Do not use exit.)
JFrames always bind to basic operating system side primitives. When they close, you need to clean up the native OS side of things. This is why closing them they way you are describing isn't a great idea.
Instead, configure the way the frame should close. Usually it is one of
setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
--or--
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
And when you decide to close the JFrame, create an event that Swing will process that indicates the window in question is closing, and send it to the dispatcher.
WindowEvent closeEvent = new WindowEvent(myFrame, WindowEvent.WINDOW_CLOSING));
myFrame.dispatchEvent(closeEvent);
This will leverage the Swing / AWT event processing code to close the window, guaranteeing all the correct closing methods are called.

How to prevent from user to open more than one JPanel in MDI Window?

I am making a vehicle_management system in java swings. I have a MDI(muti-document interface) Window in which there are more then one panels can be open.But when one panel is open after another the previous one is hides under new one, so all the JPanel are stacks. what i want is that if a panel is open and user trying to open another panel the previous opened panel get closed.how to do that.
Your application should maintain a List<JInternalFame> of open frames. In your open Action, see if the target frame is already open. If so, invoke setSelected(true) to bring the frame to the front; if not, open the frame as usual. A related example is cited here.
Addendunm:
I don't know how to make a list of open frame and check target frame is open or not.
This example illustrates how to compose and iterate a List<JInternalFrame>. Use the list's indexOf() method to search the list for an existing instance. A return value of -1 means that the list does not contain the element
First off, unless yours is an extremely unusual application, you don't "open" a JPanel. Since we're talking about an MDI application I'm guessing that what you really mean is that you're opening a new JInternalFrame.
Second, you said that you want to "close" the other panels (again, I'm assuming here that you really meant internal frames) but as someone else already pointed out, if you close them then it's not really an MDI application. I'm guessing that what you really meant or want is to MINIMIZE (not close) the other internal frames. If that's the case, then you should create a collection (e.g., a List) of JInternalFrame instances in the class where you create and add new internal frames. When you're about to add a new frame then just loop through the items in that collection and call setIcon(true) for each internal frame instance. Once that loop has completed, add the new internal frame to the collection and then also add to the desktop pane. At that point it will be the only one that's not minimized / iconified and any frame after that will likewise be (at least initially) the only one that's not minimized / iconified.

Close window - but don't stop program - JAVA

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!
You can set the defalaultCloseOperation property of the second frame to DO_NOTHING_ON_CLOSE or DISPOSE_ON_CLOSE
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?
Forget about number 1. and go straight to 2.
If using JFrame or extending it you can use setDefaultCloseOperation() method like:
frame.setDefaultCloseOperation(HIDE_ON_CLOSE);
// or
frame.setDefaultCloseOperation(DISPOSE_ON_CLOSE);
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
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

JDialog setVisible(false) vs dispose()

Does it make sense to use setVisible(false) on a dialog and reuse it later or is safer to call dispose() every time and to make a new JDialog.
What about memory leaks with setVisible(false)?
EDIT:
My Question isn't so much about quitting the application. More about Dialogs that have the main frame as parent and are opened and closed during the application life time. E.g. let's say my applications has about 10 dialogs that display different data every time I open them. Should I reuse the instances and use setVisible() or should I make a new Dialog every time and dispose() them on closing.
I would recommend using dispose() to release resources and free up memory. If you want to show the dialog again, simply invoke setVisible(true).
It's important to note that when the last displayable window within the Java virtual machine (VM) is disposed of, the VM may terminate. See AWT Threading Issues for more information.
I still can't see any diference between JDialog#dispose(); and JDialog.setVisible(false) more here, each of them could be wakeup for reuse, and doesn't matter if was/were Disposed or Visibled
my view is that this question must be splited to three separates areas
1) some JFrame is parent for JDialog or JWindow (exist only is is there JFrame), then last one must to turn-off the lights
2) without parent for JDialog
3) there still exist another JFrame, JDialog or JWindow, then last one must to turn-off the lights
reachable by using --> Window[] wins = Window.getWindows();
last one must to turn-off the lights --> System.exit(0);
I suggest that there in all of possible cases must exist visible JFrame with JFrame.EXIT_ON_CLOSE, or another way could be implements WindowsListener with System.exit(0);
Calling dispose() frees the resources associated with the dialog. You can keep the dialog around after you dispose() it. If you are worried about having too many dialogs laying around, use a WeakReference to hold the reference. That will ensure that your dialogs you are not using only survive garbage collection as long as the space they occupy is not needed.
I experienced a difference when a window shall be hiden twice (in cause of bad software design e.g.)
If you dispose an already disposed window the VM hangs. (java 8)
If you setvisible false on an already invisible window life goes on...

Categories