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

Problem: the user starts long work with the applet; JDialog is displayed with a progress bar. The user opens / switches to another browser tab. JDialog is still displayed (and annoying the user).

JDialog should be hidden when the user switches to another tab; and is displayed again when the user switches back.

Note. I saw a question with a similar problem where the solution added a windowActivated / deactivated listener. This does not work for me, because there are several frames in the window, and one of them contains an applet. When the user clicks on another frame, the windowDeactivate event is fired, but the user is still on the same tab.

+4
source share
2 answers

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
source

Try specifying the applet as the owner of the dialog box:

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

where "this" is a japplet. Hopefully this activates / deactivates the dialog every time the parent loses focus.

+3
source

All Articles