切换浏览器选项卡时隐藏applet的JDialog

时间:2013-03-03 06:39:52

标签: java javascript swing jdialog japplet

我的问题似乎与How to hide JDialog from JApplet when user switch browser tab?非常相关但它没有提供有效的解决方案。

我开发了一个使用JWS / JNLP部署的applet。首先显示“新游戏”对话框。

private class NewGameDialog extends CustomDialog implements ActionListener {
    ...
    public NewGameDialog () {
       super(topContainer, "NEW GAME", modalityType);

       System.out.println(topContainer + " " + modalityType);
       //prints JApplet and DOCUMENT_MODAL
    ...

CustomDialog只是扩展JDialog

public class CustomDialog extends JDialog implements WindowListener {

    public CustomDialog(Container cont, String title, ModalityType modalityType) {
        super(windowForComponent(cont), title, modalityType);
    }

    public static Window windowForComponent(Component c) {
        if (c instanceof Window) return (Window) c;
        return SwingUtilities.windowForComponent(c);
    }

    ...

这是从GUI类调用JDialog的方式:

public void requestNewGame () {
    SwingUtilities.invokeLater(new Runnable() {
        @Override
        public void run() {
            NewGameDialog dialog = new NewGameDialog();
            dialog.setVisible(true);
        }
    });
}

enter image description here

我使用了How to Use Modality in Dialogs中描述的不同类型的模态。当用户切换到浏览器中的另一个选项卡时,目标是隐藏JDialog。但是没有一个选项似乎不起作用。对话框在所有选项卡上方浮动。

请建议当用户移动到另一个标签时如何隐藏对话框,并在用户返回我的小程序标签后再次显示?

1 个答案:

答案 0 :(得分:4)

这适用于假设父组件在标签更改时更改屏幕上的位置。 E.G。

JDialog.getParent().getLocationOnScreen()

经测试&见过:

  • FireFox 19.0
  • Chrome版本25.0.1364.97 m

失败:

  • Internet Explorer 8.0.7601.17514

使用Dialog.ModalityType以外的其他MODELESS值拒绝访问浏览器选项卡或导致安全例外。

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class DialogApplet extends JApplet {

    @Override
    public void init() {
        Runnable r = new Runnable() {
            @Override
            public void run() {
                initGUI();
            }
        };
        SwingUtilities.invokeLater(r);
    }

    public void initGUI() {
        JButton b = new JButton("Show Dialog");
        ActionListener listener = new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent ae) {
                showDialog();
            }
        };
        b.addActionListener(listener);
        add(b);
    }

    private JDialog d;
    JTextArea output;
    int currentX = -1;
    Timer timer;

    public void showDialog() {
        if (d==null) {
            output = new JTextArea(5,20);
            Window w = SwingUtilities.windowForComponent(this);
            d = new JDialog(w, "Dialog", Dialog.ModalityType.MODELESS);
            //Dialog.ModalityType.TOOLKIT_MODAL);  //security
            //Dialog.ModalityType.DOCUMENT_MODAL);
            //Dialog.ModalityType.APPLICATION_MODAL);
            d.add(output, BorderLayout.CENTER);
            ActionListener al = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    StringBuilder sb = new StringBuilder();


                    sb.append("parent location: \t" +
                        d.getParent().getLocation() + "\n");
                    sb.append("parent location - screen: \t" +
                        d.getParent().getLocationOnScreen() + "\n");

                    System.out.print(sb.toString());

                    output.setText(sb.toString());
                    if (currentX>-1 && 
                        currentX!=d.getParent().getLocationOnScreen().getX() 
                        ) {
                        System.out.println( "Goodbye world!" );
                        d.setVisible(false);
                        timer.stop();
                    }
                }
            };
            timer = new Timer(1000, al);
            d.pack();
            d.setLocationRelativeTo(d.getParent());
        }
        currentX = (int)d.getParent().getLocationOnScreen().getX();
        timer.start();
        d.setVisible(true);
    }
}

Java脚本

或许转而向JS寻求帮助。诀窍似乎在于检测HTML窗口焦点/模糊事件。例如。详见答案:

显然,当JS检测到标签更改时,我们会让它调用applet中的方法,例如tabVisible()tabHidden(),以便在对话框中执行相应的操作。