How to show popup on other popup? - java

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)?

Related

Swing Invoke Later Events vs Modal Dialogs

I created a Modal Dialog by extending JDialog. As soon as setVisible(true) gets called code flow gets stuck at that point until the dialog gets dispose (as per expectation).
However i do not know/understand what happens to all the events already scheduled by using SwingUtilites.invokeLater(), does these events also get hold up until modal dialog closes or they are independent and do get executed when the modal dialog is still open ?
Thanks in advance.

Window Close Listener And setDefaultCloseOperation

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.

Changing something in a parent window from a child window in Java

I am wondering how you can change something in a parent window using a child window. So, let's say I have a window, that opens a pop-up window on the press of a button. That new window contains a JTextArea and another button called Ok. How can I change the title of the parent window based on what's inside the JTextArea when the Ok button is pressed?
The way I see it, there are two common ways to do this:
The child window could push the information into the parent window when OK is pressed. For this to occur, the child window would need a reference to the parent window and then call a public method of the parent.
The parent window could pull the information from the child. This can occur in one of two ways.
If the child window is a modal dialog, then the parent could simply query the child window once the dialog has been dealt with and program flow returns to the parent's code.
If the dialog is a non-modal dialog, then the parent window would need to add a listener to the child, say a PropertyChangeListener, and when the appropriate event is triggered by this listener, the parent window would query the dialog window for the information. This would be an example of using the Observer design pattern.
I much prefer the pull technique since because it is the parent that is the object that has code to display the child window and that needs the information from the child window, it should have the code to extract the information needed, and the child window would need to have no knowledge of or reference to the parent window object. This seems much cleaner to me.
For an example of this, please check out my code here, here and here.
Edit: Note that for the example that you described, you could easily solve this by using a JOptionPane. Many don't know that JOptionPanes can display very complex GUI's; basically anything that can be put onto a JPanel can be displayed in a JOptionPane (and then some). You would simply display the JOptionPane that shows your JTextField, and when program flow returns to the calling program, simply get the text held by the JTextField that was displayed in the JOptionPane. Nothing could be simpler.

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

JDialog box not gaining focus

I have a modeless dialog box being generated which prompts users to open a new window. The box can be opened in two ways, either directly from the file menu for the frame I'm writing or indirectly via the framework my panel is plugging into.
When I make the call directly via the file menu the dialog box comes up with focus exactly as I want. But when I have the framework indirectly open the dialog box it does not have focus as it should.
There doesn't seem to be a difference between the two methods of opening the dialog, in both cases a load function is called and it's not until 5 method calls later the dialog box is opened. In both cases the frame which generates the dialog box is realized at the time the box is generated. I've tried calling requestFocus after making the dialog box visible but it doesn't seem to do anything.
Any suggestion why the dialog box wouldn't have focus, or how I can give it focus as a separate window from the window that usually has focus?
in some cases is hard to set Focus to the expected top-level container as are demonstrated here , but for excelent workaround would be better look at camickr's Dialog Focus
When you create the dialog, try setting the main GUI as parent of the dialog.
In the first case, when you click from menu, it automatically sets the main GUI as the parent of the dialog, but it doesnt in the second case.
So make sure when you create the dialog, you are setting the main GUI/ window as parent always.
It should help most times.

Categories