问题
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