Why does my application terminate if user closes separate window? - java

I have a simple GUI program that creates a new window (which contains a JTable) at some point in time. If the user closes the JTable, the main application closes as well. How do I prevent this? Would it have something to do with how it handles the window closing, or should I give it it's own thread, or what?

Set one of these close operation for your JFrame: HIDE_ON_CLOSE or DISPOSE_ON_CLOSE.
You are using EXIT_ON_CLOSE.
Here is a link to the JavaDoc method you can use.

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.

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

Window closing and single instance of JFrame

Two + 1 Questions:
I've an stand alone application and created a runnable jar for it. Now, when I double-click it, a JFrame window opens and I need to log in. The problem is that I can open multiple JFrame windows if I double-click on it. What I need is once I have logged in, anytime I try to open it again, it should show me the currently logged in window. Or to put it this way, create a single instance of that JFrame window.
When closing the window, I need to do some operations before closing. I know that I can use a WindowListener but this does not work when I shutdown the JFrame (as in terminate the application). Is there a way to do any action before terminating manually?
EDIT : Another question:
I did a setExtendedState(JFrame.ICONIFIED); for minimizing the JFrame window when 'x'(close) is clicked. It minimizes but vanishes from the task bar. Is there a way to keep in the task bar like how the normal minimize works?
1)
Have your main method search for another instance of the program. If one is found, focus that window and have it close itself. You can read more about that in a similar question here: Question
2)
Use a shutdown hook:
Runtime.getRuntime().addShutdownHook(new Thread() {
#Override
public void run() {...} //your code to run when closing the program
}

Java Swing close ONLY one application

Hi all:
I have a Java Swing App. There is a button allow user to create open up a new window of the application. I use System.Exit(0) when user decides to close the application, however when I press "Close" button, both Application windows closed.
public static void main(String[] args)
{
ghMain = new GreenHouseMain();
}
Above is how I initialize the first application, then use the same code to create new GreenHouseMain Object to open second application window.
So my question is how do I close only one application window which the close button I pressed from?
Thanks all
call dispose() instead of System.exit() on the Window object that you want to close. When there are no more visible windows, the Event Dispatch thread will exit.
I assume that both windows are JFrames. If so, it is better to have the second window be a JDialog, modal or non-modal depending on your requirements. If you need both windows open and want to be able to let the user select which to close, then perhaps both need to be dialogs, though I'm not 100% sure based on the information you've presented. If these suggestions don't solve your problem, then please provide us with more details on your exact requirements.
read the javadocs for setDefaultCloseOperation. System.exit() is doing exactly what it's supposed to, so get rid of it.

Categories