【问题标题】:Hide JDialog of applet when browser's tab is switched切换浏览器选项卡时隐藏小程序的JDialog
【发布时间】:2013-03-03 06:39:52
【问题描述】:

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

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

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

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

请告诉我如何在用户移动到另一个选项卡时隐藏对话框并在用户返回我的小程序选项卡后再次显示?

【问题讨论】:

  • 如需尽快获得更好的帮助,请发帖SSCCE
  • 注意:添加 JavaScript 标记是因为我认为无法使用纯 Java 提供功能,而现有的解决方案是通过 JS。

标签: java javascript swing jdialog japplet


【解决方案1】:

这假设父组件在选项卡更改时更改屏幕上的位置。例如

JDialog.getParent().getLocationOnScreen()

经过测试和证明可以工作:

  • 火狐 19.0
  • Chrome 版本 25.0.1364.97 m

失败:

  • Internet Explorer 8.0.7601.17514

使用除MODELESS 之外的其他Dialog.ModalityType 值会拒绝访问浏览器选项卡或导致安全异常。

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 检测选项卡更改时,我们会让它调用小程序中的方法,例如 tabVisible()tabHidden(),以对对话框执行适当的操作。

【讨论】:

  • 谢谢!我正在使用 Chrome,顺便说一句。我今晚或明天可以测试这个例子,如果它适合我​​,我会接受。
  • "如果可行则接受" 在 IE 中失败。见更新。我也在窗口和按钮上尝试了一个组件侦听器。触发的唯一事件是初始外观,而不是选项卡更改。 JS可以检测到标签的变化吗?如果是这样,这是最好的方法。
猜你喜欢
  • 2011-11-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-03-19
相关资源
最近更新 更多