简体   繁体   English

如何使JDialog不总是在父级之上

[英]How to make a JDialog not always on top of parent

I have a JFrame that spawns two JDialogs. 我有一个产生两个JDialog的JFrame。 Each of the three windows needs to be focusable (and are the way I currently have it written) but that JFrame won't go atop the dialogs. 三个窗口中的每一个都需要是可聚焦的(并且是我目前编写的方式),但JFrame不会在对话框的顶部。 When you click on either dialog, they'll pop on top of each other (like one would expect), but that JFrame just refuses to come to the front. 当你点击任一对话框时,它们会互相弹出(就像人们期望的那样),但JFrame只是拒绝出现在前面。

I need them to remain JDialogs (as opposed to being JFrames themselves) since most of the current behavior is desirable (ie when another window/application blocks any or all of the windows, if you select any of the windows they all come to the front (whereas three JFrames would result in only the selected one coming forward)). 我需要它们保持JDialogs(而不是JFrames本身)因为大多数当前行为是可取的(即当另一个窗口/应用程序阻止任何或所有窗口时,如果你选择任何一个窗口它们都会出现在前面(而三个JFrame只会导致选定的一个JFrame))。

My JDialogs constructors are to this effect: 我的JDialogs构造函数就是这样的:

SubDialog(JFrame parent /*, a handful, ofOther arguments */){
    super(parent, ModalityType.MODELESS); //not even the modeless helped
    setAlwaysOnTop(false); //not even the not always on top helped
    setUndecorated(true); //maybe this has something to do with it (unlikely, just fyi)?

    //some simple variable assignments

}

I even tried tossing a setAlwaysOnTop(true) in my JFrame. 我甚至尝试在我的JFrame中抛出setAlwaysOnTop(true) No dice. 没有骰子。 I was getting desperate and even tried one of these numbers: 我变得绝望,甚至尝试了其中一个数字:

MyJFrame(String title){
    super(title);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    addWindowFocusListener(new WindowAdapter(){
        public void windowGainedFocus(WindowEvent e){
            final Window w = e.getWindow();

            //PLEASE come to the front
            w.toFront();

            //even MOAR desperation
            SwingUtilities.invokeLater(new Runnable(){
                public void run(){
                    w.toFront(); //STILL no dice.
                }
            });
        }
    });
}

Thoughts? 思考? I got nothin'. 我什么都没有。

How to make a JDialog not always on top of parent 如何使JDialog不总是在父级之上

As stated in this Q&A: setModal issue with 2 Jdialogs within a Jframe : 正如本问答中所述:在Jframe中有2个Jdialog的setModal问题

This behaviour depends on how native windowing system handles focused and active windows. 此行为取决于本机窗口系统如何处理聚焦和活动窗口。 Having said this if you call for instance toFront() it will attempt to place the window at the top of the stack BUT some platforms do not allow windows which own other windows to appear on top of its children. 如果你调用例如toFront(),它会尝试将窗口放在堆栈的顶部但是有些平台不允许拥有其他窗口的窗口显示在其子节点之上。 The same happens when you call toBack() method. 调用toBack()方法时也会发生同样的情况。 See the javadocs for more details. 有关更多详细信息,请参阅javadocs。

For instance on Windows 7 parent dialog becomes focused but its children still showing (not focused) at the top. 例如,在Windows 7上,父对话框变得聚焦,但其子代仍显示(未聚焦)在顶部。 As mentioned above it's up to the windowing system decide how to handle this matter. 如上所述,窗口系统决定如何处理这个问题。

It is very easy to achieve this, see the following code: 这很容易实现,请参阅以下代码:

    JFrame frame = new JFrame();
    frame.setBounds(0,0,400,200);
    frame.setExtendedState(JFrame.MAXIMIZED_BOTH);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);

// Special attention to this line, do not use same JFrame, create a dummy JFrame
// If you want to save memory you can also use new JDialog((JFrame)null)
    JDialog jd = new JDialog(new JFrame());
    jd.setModalityType(Dialog.ModalityType.MODELESS);
    jd.setBounds(0,0,100, 100);
    jd.setVisible(true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM