Model dialog opened from Applet is placed behind A

2019-08-13 18:52发布

The applet consists of following code:

public class TestApplet extends Applet {
public TestApplet() {
    EventQueue.invokeLater(new Runnable() {
        @Override
        public void run() {
            JDialog dialog = new JDialog();
            dialog.setContentPane(new JLabel("Hello"));
            dialog.setSize(new Dimension(300, 200));
            dialog.setModal(true);
            dialog.setVisible(true);
        }
    });
}}

When I open it on InternetExplorer running on Windows 7 it works: I change browser tabs, dialog always stays in front.

When I open it on Firefox ESR 10.0.5 running on Red Hat Enterprise Linux Server Release 6.3, Java 1.7.0_07-b10 then it instantly goes behind the Browser window and I have to minimize browser in order to find it again.

What do I have to do to make the modal dialog always stay in front of the Applet?

Update:

Changing creation of JDialog to

JDialog dialog = new JDialog(javax.swing.SwingUtilities.getWindowAncestor(TestApplet.this));

makes not difference.

1条回答
该账号已被封号
2楼-- · 2019-08-13 19:23

Finally, after trying alot of things I figured out the following workaround:

public class ModalDialog extends JDialog {

    private boolean isClosing = false;

    protected synchronized boolean isClosing() {
        return isClosing;
    }


    protected synchronized void setClosing(boolean isClosing) {
        this.isClosing = isClosing;
    }

    public ModalDialog() {
        setSize(200, 300);
        setDefaultCloseOperation(JDialog.DISPOSE_ON_CLOSE);

        addFocusListener(new FocusAdapter() {
            public void focusLost(FocusEvent arg0) {
                if (isClosing()) {
                    System.out.println("Returned because dialog is already closing");
                    return;
                }
                EventQueue.invokeLater(new Runnable() {
                    public void run() {
                        ModalDialog.this.setVisible(false);
                        ModalDialog.this.setVisible(true);
                    }
                });
            }
        });
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.out.println("Dialog is closing");
                setClosing(true);

            }
        });
    }
}
查看更多
登录 后发表回答