The modal dialogue is hidden behind the main frame after swich focus

I have a swing application, basically the main frame, which can bring up a modal dialog. When a modal dialog is displayed, if I switch to another window, for example, firefox. Then go back to the download application. JDialog no longer in front.

I do not want the AlwaysOnTop dialog to be right. because then the dialog will be on top of all windows, including windows in another process.

So what should I do so that when I go back, the modal dialogue is still on top?

BTW: this is an applet, so the main frame is actually set this way:

 private static Frame findParentFrame(Container owner){ Container c = owner; while(c != null){ if (c instanceof Frame) return (Frame)c; c = c.getParent(); } return (Frame)null; } 
+4
source share
4 answers

I am not sure that the modality of the dialogue is of key importance here. I tested this behavior, and the dialog always appears on the front panel when the application is maximized regardless of its modality.

 import java.awt.event.ActionEvent; import javax.swing.JApplet; import javax.swing.SwingUtilities; import java.awt.Color; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Frame; import java.awt.event.ActionListener; import javax.swing.JButton; import javax.swing.JDialog; public class AppletTest extends JApplet implements ActionListener { private static final long serialVersionUID = 1L; private Frame findParentFrame() { Container c = this; while(c != null) { if(c instanceof Frame) return (Frame) c; c = c.getParent(); } return (Frame) null; } private void createGUI() { Container content = getContentPane(); content.setBackground(Color.white); content.setLayout(new FlowLayout()); content.add(new JButton("Button 1")); content.add(new JButton("Button 2")); content.add(new JButton("Button 3")); JDialog d = new JDialog(findParentFrame()); d.setModal(true); d.setVisible(true); } public void init() { try { SwingUtilities.invokeAndWait(new Runnable() { public void run() { createGUI(); } }); }catch(Exception e) { System.err.println("createGUI didn't successfully complete"); } } @Override public void actionPerformed(ActionEvent e) { } } 

Check out the example I provided. You can comment a line with d.setModal(true); , and the result will be the same. I would suggest you check your code again or show it to us, because it seems like you missed something.

PS: I found on the Internet some other hacker solution http://www.codeguru.com/forum/showthread.php?t=41536 I would still focus on checking your code.

Oh and good luck, Boro.

+1
source

Make sure JDialog is actually modal. Also try setting the main frame as the owner.

+2
source

I think you are requesting a dialog that will be modal for a Java application / frame that is parent. When the parent regains focus, you can use Toolkit.getDefaultToolkit (). GetSystemEventQueue (). postEvent (AWTEvent e) to trigger an event in a dialog so that it pops up.

0
source

Thanks Boro for the link

I had an identical problem that I needed to solve. Browser with a swinging applet. The dialog pops up, I click on the browser, click on the dialogue and the dialogue disappears behind the browser. I tried everything, but only one helped:

Adding a WindowListener to Dialog and calling toFront() in a windowDeactivated() listener worked for me.

0
source

Source: https://habr.com/ru/post/1347775/


All Articles