Window Close Listener And setDefaultCloseOperation - java

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.

Related

How to check if a JFrame is in existence?

I am making an application where I need to have a Preferences JFrame which can open another JFrame for further preferences of a certain type. Though I do not want the further preferences JFrame to be able to have more than one instance open at the same time.
I have tried using the .isActive() method but that always no matter what happens reports that the variable used to make the new instance of the JFrame is equal to null, which it has been initially set to when it was initialised.
So what is the best way of actually checking if a certain JFrame already has an instance in existence? Is it to get it to write to a variable when it launches and when it quits? If so, how would you get it to write to a variable when someone pressed the x button, and what if it crashed and closed not through the standard means?
Or is there another way of doing it like getting the isActive() method to work or another method that would do better?
What you should do is to make the JFrame a modal JDialog. Instead of opening a JFrame, you open a JDialog. You pass the other settings window as the parent and pass true for modal. This will prevent any action in the other settings window as long as this modal dialog is open.
Thats what a lot/almost every program uses. Have you ever noticed that, when you open a settings window and try to click on the main window, the settings window pops back on top and blinks for a second? Thats what the modal dialog does.
Why don't you just avoid creating a new JFrame at all? Just make one earlier in your execution and make it visible when you need it and hide it when you temporarily don't need it anymore. Trying to make an already visible JFrame visible or hide a hidden JFrame shouldn't do anything.

Parent Window Closing when Closing Child Window

I have an application that creates a new window (let's call it Cuprins) when a button of the same name is pressed. The problem I have is that when I close the new window, it also closes the main one. Is there anyway to make the main window not close when closing the Cuprins window?
Check what's the default close operation. Set it to "dispose on close" or "do nothing on close"
The newly opened window should use dispose on close to dispose the frame, or setVisible(false) to hide it temporarily.
Just use this line while you creating child window.
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
Usage example :
AddLeagues addLeague = new AddLeagues(); //Child View
addLeague.setVisible(true);
myFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

Starting an event after closing JFrame window

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
}
});

Problems with the modality of a JDialog (mouse events for main window both fired and buffered)

I made a JDialog and the modality works presumably fine
dialog.setModalityType(JDialog.ModalityType.APPLICATION_MODAL);
dialog.setVisible(true);
But then my problem is:
I´m throwing Jdialog after a jcombobox.setSelection() and I need to click twice in Accept button in order to hide the dialog, because dropdown popup is consuming the first click for closing himself. I fixed it by manually calling jcombobox.hidePopup() before calling the dialog, but I cannot understand if the later is modal, why the mouse events trigger things outside the window?`
My Main window buffers somehow the mouse events, so for those mouse events which are not activated when the modal dialog is drawn (as happens with the previous point), it seems they get buffered and are applied after dialog closure. Is this an expected behavior?
Thank u!
replace jcombobox.hidePopup("doesn't make me sence") with ActionListener or ItemListener added to the JComboBox
add RequestFocusListener by #camickr for setting the FocusOwner correctly
for why reasons are there another MouseListeners, maybe in the case that fird any events to the JComponents that you can't to set Focus correctly

How to show popup on other popup?

In my application, on some screen, I launch a popup. Depends on what button the user will click on this popup, another popup has to be launched. I use JDialog object to implement these popups. The issue is that the second popup doesn't show up (even though is use setVisible(true) and toFront()). It's created but I can't see it. I defined in its constructor the first popup as its owner. Anyone can help?
When a JDialog is opened from a parent window or dialog, and set to be modal, the Event Dispatch Thread for the parent window is halted. This prevents the parent from being focused or from passing other events, or essentially doing anything until the modal dialog is closed. The call is therefore blocking.
What you must do instead is fire your event from somewhere else, like the new dialog instead of the parent window, OR instead of using modal dialogs, use a regular JFrame and set it to be always on top using setAlwaysOnTop(true). That means the user can continue to use the parent window, and events will still fire from it.
Addendum: in response to your problem "the program concentrates in showing it and doesn't react to the event that has to hide it": when you make a dialog modal, as soon as you make it visible, it will block the parent window until it is closed, including event firing. If you need the new popup to be closed programatically, you either need to make the popup non-modal, or you need to execute the subsequent code in the context of the new popup window (such as firing an event when it becomes visible)
OK, now I managed to show the second popup. The code in the event that triggers the popup is:
printingWindow.setLocationRelativeTo(null);
printingWindow.toFront();
printingWindow.setModal(true);
printingWindow.pack();
printingWindow.setVisible(true);
But now I have a different problem:
when the printingWindow is set to be visible, the program concentrates in showing it and doesn't react to the event that has to hide it.
The code that is executed when the appropriate event is fired is:
printingWindow.setVisible(false);
printingWindow.dispose();
So how I close this popup (by firing the event)?

Categories