开发者

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

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 m开发者_如何学Goe, 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.


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.


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>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜