Вопрос

I have a JFrame that spawns two JDialogs. 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. 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.

I need them to remain JDialogs (as opposed to being JFrames themselves) since most of the current behavior is desirable (i.e. 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)).

My JDialogs constructors are to this effect:

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. 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

As stated in this Q&A: setModal issue with 2 Jdialogs within a Jframe:

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. The same happens when you call toBack() method. See the javadocs for more details.

For instance on Windows 7 parent dialog becomes focused but its children still showing (not focused) at the top. 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 с атрибуция
Не связан с StackOverflow
scroll top