切换焦点后模态对话框隐藏在主框架后面

发布于 2024-11-01 00:41:04 字数 516 浏览 3 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

泪痕残 2024-11-08 00:41:04

确保 JDialog 实际上是模态的。还可以尝试将主框架设置为所有者。

Make sure that the JDialog is actually modal. Also try setting the main frame as the owner.

蹲墙角沉默 2024-11-08 00:41:04

我不确定对话的方式是否是这里的关键问题。我已经测试了这种行为,当应用程序最大化时,无论它是否处于模式状态,对话框总是会在前面弹出。

import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;

public class AppletTest extends JApplet
        implements ActionListener
{
    private static final long serialVersionUID = 1L;
    private Frame findParentFrame()
    {
        Container c = this;
        while(c != null)
        {
            if(c instanceof Frame)
                return (Frame) c;

            c = c.getParent();
        }
        return (Frame) null;
    }
    private void createGUI()
    {
        Container content = getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout());
        content.add(new JButton("Button 1"));
        content.add(new JButton("Button 2"));
        content.add(new JButton("Button 3"));
        JDialog d = new JDialog(findParentFrame());
        d.setModal(true);
        d.setVisible(true);
    }

    public void init()
    {
        try
        {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    createGUI();
                }
            });
        }catch(Exception e)
        {
            System.err.println("createGUI didn't successfully complete");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
    }
}

查看我提供的示例。您可以使用 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.

import java.awt.event.ActionEvent;
import javax.swing.JApplet;
import javax.swing.SwingUtilities;
import java.awt.Color;
import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JDialog;

public class AppletTest extends JApplet
        implements ActionListener
{
    private static final long serialVersionUID = 1L;
    private Frame findParentFrame()
    {
        Container c = this;
        while(c != null)
        {
            if(c instanceof Frame)
                return (Frame) c;

            c = c.getParent();
        }
        return (Frame) null;
    }
    private void createGUI()
    {
        Container content = getContentPane();
        content.setBackground(Color.white);
        content.setLayout(new FlowLayout());
        content.add(new JButton("Button 1"));
        content.add(new JButton("Button 2"));
        content.add(new JButton("Button 3"));
        JDialog d = new JDialog(findParentFrame());
        d.setModal(true);
        d.setVisible(true);
    }

    public void init()
    {
        try
        {
            SwingUtilities.invokeAndWait(new Runnable()
            {
                public void run()
                {
                    createGUI();
                }
            });
        }catch(Exception e)
        {
            System.err.println("createGUI didn't successfully complete");
        }
    }

    @Override
    public void actionPerformed(ActionEvent e)
    {
    }
}

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.

转身泪倾城 2024-11-08 00:41:04

我认为您要求的是一个对于其父级 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.

無心 2024-11-08 00:41:04

感谢 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 to Dialog and call to toFront() in listener's windowDeactivated() worked for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文