After i close my window,i want to create an object of a class and preform a task.
After closing:
setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
What can i do so that it can start a new operation as soon as window is closed?
please help.
When the user tries to close a JFrame, the following events happen :
the windowClosing method of the WindowListeners registered on that frame are invoked.
then, the action specified by setDefaultCloseOperation is triggered.
There are 4 default close operations :
do nohting on close
...does nothing at all. The frame is still here, happily ignoring your requests to close it.
hide on close
Same effect as calling setVisible(false) on the frame. The frame becomes invisible, and it can be displayed again by calling setVisible(true).
dispose on close
Same effect as calling dispose() on the frame. The effect is somewhat similar to HIDE_ON_CLOSE, the difference is that this time, the OS resources used by the frame are released. You can still call setVisible(true) again if you want to make the frame appear again.
The windowClosed method of the WindowListeners registered on that frame are invoked.
Also, note that when the last displayable window is disposed of, your program may terminate.
exit on close
Same effect as calling System.exit().
So, if you want to have full control on what happens when the user tries to close a frame, set the default operation to DO_NOTHING_ON_CLOSE, and put your code in the windowClosing method of a WindowListener. There, you can call setVisible(false), dispose() or System.exit(), depending on what you want to achieve.
You can also set the default operation to hide, dispose, or exit on close, and put your code in the windowClosing method of a WindowListener. In this case, the listener will be invoked first, and then the chosen action will be executed, no matter what.
Finally, you can also set the default operation to DISPOSE_ON_CLOSE, and put your code in the windowClosed method of a WindowListener. That way, the frame is first disposed (it disappears from the screen), and then your code gets executed.
After i close my window,i want to create an object of a class and preform a task.
look at
HIDE_ON_CLOSE simplair as setVisible(false);, container is re_usable, is possible to show it by calling setVisible(true);
or you can to call dispose(), meaning that this container isn't re_usable, have to check isDisplayable(), before calling setVisible(true);
i want to create an object of a class and preform a task
see SystemTray, maybe could be proper way
Call
frame.setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);
and register a listener for the closing event:
frame.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
JFrame frame = (JFrame)e.getSource();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// additional code here
}
});
Related
I am calling dispose method and also setting setDefaultCloseOperation for the frame.
However, after dispose being called upon, i checked with Dialog.getallwindows() the frame is not actually killed. Is there any alternative way to dispose frame
I have a question about the relationship between swing window listener and default close operation. The question occurs when I am dealing with below scenario:
I add a window listener (WindowAdapter is used for the listener) for a JFrame, and override the windowClosing function: if the user closes the window, a dialog will pop up to confirm, if user chooses CANCEL option, then directly return. However, when I test the code and choose CANCEL when closing the window, the frame window is still closed (or maybe just invisible, because the Java icon is still in the task bar).
Then I add the default close option with DO_NOTHING_ON_CLOSE, with the same test behavior, the frame window is not closed, which is what I expected.
setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
Then I change the default close option with EXIT_ON_CLOSE, with the same test behavior, the frame window is directly closed(this time Java icon also disappeared).
This makes me confused. Does it mean the window listener can just define what to do when the window is closing but can't determine whether to close the window? Or I need to override other function?
The window listener is just a listener. It does not affect the default close operation, unless you actually change the default close operation in the windowClosing() code.
I use:
frame.setDefaultCloseOperation(DO_NOTHING_ON_CLOSE);
as the default when the frame is created. Then in the windowClosing(...) method, if the user confirms the closing of the frame I change the default:
frame.setDefaultCloseOperation(EXIT_ON_CLOSE);
Check out Closing an Application for more information and more complete example code.
I run my main window in the main method like this:
SwingUtilities.invokeLater(new Runnable() {
#Override
public void run() {
NoteSystem MainWindow = new NoteSystem();
MainWindow.initUI();
}
});
And then when a button is pressed on this form, I create a window by instantiating a class I made. I'm having trouble detecting when the second form is closed and what the textboxes/other controls contained.
What is the proper way to:
a) Fire an event in NoteSystem when the second window is closed
b) allow NoteSystem to check all the components/controls in the second window
I considered using a JOptionPane, but I'd like to create the window entirely in my own class. The idea of having the main window frozen while waiting for response from the second window works for my application though, so if I could use JOptionPane with my own class, that would be ideal.
Thanks
The best way is to use a modal dialog, a window like a JFrame, but that halts program flow in the calling code until it is no longer visible. This way, the calling code will know exactly when the dialog window has been dealt with, since its code flow will resume once again, and so often the calling code will extract information from the dialog window code at that point. A JOptionPane is one type of these, and so is a modeal JDialog (of which a JOptionPane is a sub-type). Either of these can display as complex a GUI as any that is displayed within a JFrame, so don't sell them short. You'll notice that the second parameter of most JOptionPane methods is of type Object, meaning anything can go in there, but most often you'll pass in either a String for a simple JOptionPane, or a JPanel that can be chock full of components and other nested JPanels, and in this way the JOptionPane can display the complex GUI if need be.
For examples, please see:
Passing values between JFrames
action listener to JDialog for clicked button
trouble obtaining variable info from another JFrame
How do you return a value from a java swing window closes from a button?
I have a very simple JFrame window that contains one button: No.
In the main function I set setVisible(true); my JFrame and in the No button listener I want to close the window so I set the visibility to false: setVisible(false); and after that I do System.exit(0); in order to prevent possible memory leaks when running the program many times.
I have two questions:
Do I really need to System.exit(0); in the above case?
If I have this JFrame as a popup window, I can't really use System.exit(0); because this will terminate the whole program. So how can I properly close the popup window and stay in the main JFrame window? (Now I close it only by setVisible(false); and when I do it several times through the program execution, the program turns very slow).
use CardLayout
if is there real reason for another popup container
use JDialog with parent to JFrame, with setModal / ModalityTypes
create only one JDialog and to reuse this one JDialog by getContentPane#removeAll()
use JOptionPane for simple users interaction
put both together, above two points, to use CardLayout for popup JDialog with parent to JFrame, notice after switch from one card to another could be / is required to call JDialog.pack()
setVisible will cause slowdown
dispose will cause slowdown
System.exit will close entire JVM
Therefore, you should reuse a single JFrame or JDialog.
In the button's ActionListener, invoke frame.setVisible(false);. Then instead of creating a new frame just do frame.setVisible(true);. If you want to change the contents of the frame, there is the function frame.getContentPane().removeAll();.
Just add this: JFrame.setDefaultCloseOperation(DISPOSE_ON_CLOSE).
Note: The default option for JFrame is HIDE_ON_CLOSE.
You can use the dispose() method of the JFrame class to close the frame and release all resources associated with it, including its child components.
how do i close a frame yet open a new frame?
i have a frame, (help)
when i click on my menu item
i want to open (mainForm)
exit from help.
new mainForm().setVisible(true);
System.exit(0);
i know this closes the whole program however how do i get it to only close the current frame
thanks
If you no longer want to use the frame you could use frame.dispose()
If you just want to hide it use frame.setVisible(false).
If you extended a Frame and are trying to close it from within use this.dispose or this.setVisible(false).
You should rethink your requirments. For the user, it would be best to have both the program and the help window visible at the same time. Closing the main window when showing the help screen and vice versa is really, really bad for usability - you shouldn't do it. We've had window-based GUIs for almost 30 years now - showing several windows on screen at the same time is what they're for!
Let's say you created your frame as so:
JFrame mainframe = new JFrame("Radio Notes");
//show Frame
mainframe.setVisible(true);
//close the frame
mainframe.dispose();
I think you should hide the frame you do not wish shown with setVisible(false). System.exit(0) stops the JVM ending the entire program.
In short, the semantics of setVisible has nothing to do with starting / stopping the application.
If you want to start an entire new application you'd have to look at Runtime.exec(). I don't really know if you can close the parent process (the Help application) with exit() though.
try setting the default close operation for the JFrame like so.
frame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
Then implement a WindowListener that performs the actions you want when a window closing event is fired.