切换焦点后模态对话框隐藏在主框架后面
我有一个 swing 应用程序,基本上是一个可以弹出模式对话框的主框架。 当模式对话框显示时,如果我切换到另一个窗口,例如 Firefox。然后切换回 swing 应用程序。 JDialog
不再位于前面。
我不想将对话框 AlwaysOnTop 设置为 true。因为这样对话框将位于所有窗口之上,包括其他进程中的窗口。
那么我应该怎么做,以便当我切换回来时,模式对话框仍然在顶部?
BTW:它是一个Applet,所以主框架实际上是这样设置的:
private static Frame findParentFrame(Container owner){
Container c = owner;
while(c != null){
if (c instanceof Frame)
return (Frame)c;
c = c.getParent();
}
return (Frame)null;
}
I have a swing application, basically, a main frame that could pop up a modal dialog.
When the modal dialog is showing, if I switch to another window, like firefox. And then switch back to the swing application. The JDialog
is not in front any more.
I don't want to set the dialog AlwaysOnTop to be true. because then the dialog will be on top of all windows include windows in other process.
So what should I do so that when I swich back, the modal dialog still on top?
BTW: it is a Applet, so the main frame is actually be set in this way:
private static Frame findParentFrame(Container owner){
Container c = owner;
while(c != null){
if (c instanceof Frame)
return (Frame)c;
c = c.getParent();
}
return (Frame)null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
确保
JDialog
实际上是模态的。还可以尝试将主框架设置为所有者。Make sure that the
JDialog
is actually modal. Also try setting the main frame as the owner.我不确定对话的方式是否是这里的关键问题。我已经测试了这种行为,当应用程序最大化时,无论它是否处于模式状态,对话框总是会在前面弹出。
查看我提供的示例。您可以使用 d.setModal(true); 注释该行,结果将完全相同。
我建议您再次检查您的代码或将其展示给我们,因为您可能错过了其中的某些内容。
PS:我在网上发现了一些其他类似黑客的解决方案 http://www .codeguru.com/forum/showthread.php?t=41536
不过,我仍然会专注于检查你的代码。
奥伊&祝你好运,博罗。
I am not sure if the modality of the dialog is the key issue here. I have tested this behaviour and the dialog always popups on the front when the app is maximised independent of it being modal.
Check out the example I provided. You can comment the line with
d.setModal(true);
and the result will be exactly the same.I would suggest for you to check your code once more or show it to us as it seems that you might have missed something there.
PS: I found on the web some other hack-like solution http://www.codeguru.com/forum/showthread.php?t=41536
I would still focus on checking your code though.
Oi & Good luck, Boro.
我认为您要求的是一个对于其父级 Java 应用程序/框架来说是模态的对话框。当父级重新获得焦点时,您可以使用 Toolkit.getDefaultToolkit().getSystemEventQueue()。 postEvent(AWTEvent e) 向对话框触发一个事件,使其弹回顶部。
I think what you're asking is for a dialog that's modal to the Java application/frame that is it's parent. When the parent regains focus you can use Toolkit.getDefaultToolkit().getSystemEventQueue(). postEvent(AWTEvent e) to fire an event to the dialog to make it pop back to the top.
感谢 Boro 提供的链接!
我需要解决同样的问题。带有 Swing 小程序的浏览器。弹出对话框,我单击浏览器,单击返回对话框,对话框消失在浏览器后面。我尝试了所有方法,但只有一件事有帮助:
将
WindowListener
添加到Dialog
并在侦听器的windowDeactivated()< 中调用
toFront()
/code> 为我工作。Thanks Boro for the link!
I had identical problem I needed to solve. Browser with a swing applet. Dialog pops up, I click on the browser, click back on dialog and dialog disappears behind the browser. I tried everything, but only one thing helped:
Adding
WindowListener
toDialog
and call totoFront()
in listener'swindowDeactivated()
worked for me.