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
}
Related
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
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
Using reflection, I've managed to invoke the main method of another java application that uses swing to create windows. I've also been able to grab those windows and manipulate them. When I get to a certain point, I loop through the actionListeners of a certain JMenuItem and I call their actionPerformed telling them the button has been pressed. This works exactly as intended, and opens a new window. When this window opens, however, I want to do something similar to it by first getting the window and then the components inside of it.
However, as soon as the event is fired, the window is created and my program is put in a busy loop waiting for the window I want to interact with to close. This is caused by the application I am invoking, and I have no control over this, nor do I have an opportunity to do anything about it.
Here's how I am doing that event firing
for (ActionListener a : nc.getActionListeners()) {
a.actionPerformed(new ActionEvent(nc, ActionEvent.ACTION_PERFORMED,null) {});
}
What I'm thinking is I might want to have another thread that's looking for the window, but I'm not even sure if that will work..
Window has static method
public static Window[] getWindows() {
return getWindows(AppContext.getAppContext());
}
Frame has similar one
public static Frame[] getFrames()
So you can get copy of the created windows (frames) before your click emulation and compare with the new list after click to find the newly created one.
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.
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.