swich焦点后,模态对话框隐藏在主框架后面

时间:2021-08-31 11:20:22

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.

我有一个swing应用程序,基本上是一个可以弹出模态对话框的主框架。当模式对话框显示时,如果我切换到另一个窗口,如firefox。然后切换回swing应用程序。 JDialog不再是前面的了。

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.

我不想将对话框AlwaysOnTop设置为true。因为那时对话框将在所有窗口之上包括其他进程中的窗口。

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:

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;
}

4 个解决方案

#1


1  

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.

看看我提供的例子。你可以用d.setModal(true)注释该行;结果将完全相同。我建议您再次检查您的代码或向我们展示,因为您似乎可能错过了那些东西。

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.

PS:我在网上发现了一些其他类似黑客的解决方案http://www.codeguru.com/forum/showthread.php?t=41536我仍然会专注于检查你的代码。

Oi & Good luck, Boro.

Oi&祝你好运,Boro。

#2


2  

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

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

#3


0  

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.

我认为你所要求的是一个对象是Java应用程序/框架的模态,它是它的父级。当父级重新获得焦点时,您可以使用Toolkit.getDefaultToolkit()。getSystemEventQueue()。 postEvent(AWTEvent e)向对话框发出一个事件,使其弹回到顶部。

#4


0  

Thanks Boro for the link!

感谢Boro的链接!

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:

我有同样的问题需要解决。带有swing applet的浏览器。对话框弹出,我点击浏览器,点击返回对话框,对话框消失在浏览器后面。我尝试了一切,但只有一件事有所帮助:

Adding WindowListener to Dialog and call to toFront() in listener's windowDeactivated() worked for me.

将WindowListener添加到Dialog并在侦听器的windowDeactivated()中调用toFront()为我工作。

#1


1  

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.

看看我提供的例子。你可以用d.setModal(true)注释该行;结果将完全相同。我建议您再次检查您的代码或向我们展示,因为您似乎可能错过了那些东西。

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.

PS:我在网上发现了一些其他类似黑客的解决方案http://www.codeguru.com/forum/showthread.php?t=41536我仍然会专注于检查你的代码。

Oi & Good luck, Boro.

Oi&祝你好运,Boro。

#2


2  

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

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

#3


0  

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.

我认为你所要求的是一个对象是Java应用程序/框架的模态,它是它的父级。当父级重新获得焦点时,您可以使用Toolkit.getDefaultToolkit()。getSystemEventQueue()。 postEvent(AWTEvent e)向对话框发出一个事件,使其弹回到顶部。

#4


0  

Thanks Boro for the link!

感谢Boro的链接!

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:

我有同样的问题需要解决。带有swing applet的浏览器。对话框弹出,我点击浏览器,点击返回对话框,对话框消失在浏览器后面。我尝试了一切,但只有一件事有所帮助:

Adding WindowListener to Dialog and call to toFront() in listener's windowDeactivated() worked for me.

将WindowListener添加到Dialog并在侦听器的windowDeactivated()中调用toFront()为我工作。