Search code examples
javabrowserjdialogjapplet

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


Solution

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