How to hide JDialog from JApplet when user switch browser tab?

╄→гoц情女王★ 提交于 2019-12-30 22:59:56

问题


Problem: user starts long operation from applet; JDialog with progress bar is displayed. User open/switch to another browser tab - JDialog is still displayed (and annoys user).

JDialog should be hidden when user switch to another tab; and displayed again, when user switch back.

Note: I saw question with similar problem, where solution was add windowActivated/deactivated listener. It doesn't work for me, because there are multiple frames in window, and one of them contains applet. When user clicks on another frame, windowDeactivate event is casted, but user still in the same tab.


回答1:


Try specifying the applet as the owner of the dialog:

JDialog dialog = new JDialog(SwingUtilities.windowForComponent(this));

where "this" is the JApplet. Hopefully this will activate/deactive the dialog every time the parent loses focus.




回答2:


Solution: add listeners to all frames

<head>

    ...
    <script type="text/javascript">
        onBlur=function(event) { window.focusFlag = false; };
        onFocus=function(event){ window.focusFlag = true; };
        function createFocusListeners()
        {
            window.focusFlag = true;

            if (/*@cc_on!@*/false) { // check for Internet Explorer
                document.onfocusin = onFocus;
                document.onfocusout = onBlur;
            } else if (typeof window.addEventListener != "undefined"){
                document.getElementById('topFrame').contentWindow.addEventListener('focus',onFocus, false);
                document.getElementById('topFrame').contentWindow.addEventListener('blur',onBlur, false);
                document.getElementById('leftFrame').contentWindow.addEventListener('focus',onFocus, false);
                document.getElementById('leftFrame').contentWindow.addEventListener('blur',onBlur, false);
                document.getElementById('mainFrame').contentWindow.addEventListener('focus',onFocus, false);
                document.getElementById('mainFrame').contentWindow.addEventListener('blur',onBlur, false);
                window.addEventListener('focus',onFocus, false);
                window.addEventListener('blur',onBlur, false);
            }
        };

        //main frame is constantly reloaded, must add listener after each reload
        window.createMainFrameFocusListeners = (function () {
            if (typeof window.addEventListener != "undefined"){
        document.getElementById('mainFrame').contentWindow.addEventListener('focus',onFocus, false);
        document.getElementById('mainFrame').contentWindow.addEventListener('blur',onBlur, false);
        }
        });
    </script>
</head>


<frameset rows="32,*" cols="*" onload="createFocusListeners();">
    <frame id="topFrame" src="MenuFrame.jspx" name="topFrame" scrolling="NO" noresize="noresize"/>
    <frameset rows="*" cols="280,*">
        <frame id="leftFrame" src="TreeFrame.jspx" name="leftFrame" scrolling="NO"/>
        <frame id="mainFrame" src="ListView.jspx" name="mainFrame" scrolling="NO"/>
    </frameset>
</frameset>


来源:https://stackoverflow.com/questions/7584646/how-to-hide-jdialog-from-japplet-when-user-switch-browser-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!