Frame always on top of my program only

纵然是瞬间 提交于 2019-11-28 12:13:07

You should make your window to always on top only when the parent window goes activated. Something like this:

public class Test {
    private static JFrame mainFrame;

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainFrame = new JFrame("test");
                mainFrame.setSize(800,600);
                mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mainFrame.setVisible(true);

                final A a = new A();
                mainFrame.addWindowListener(new WindowAdapter() {
                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void windowDeactivated(WindowEvent e) {
                        a.setAlwaysOnTop(false);
                    }

                    /**
                     * {@inheritDoc}
                     */
                    @Override
                    public void windowActivated(WindowEvent e) {
                        a.setAlwaysOnTop(true);
                    }
                });
            }
        });
    }

    public static class A extends JDialog {

        public A() {
            super(mainFrame);
            setAlwaysOnTop(true);
            setFocusable(false);
            setSize(80,60);
            setVisible(true);
        }
    }
}

Ok, I found a solution (don't know if it is THE solution, but it's working, so...)

I discovered setFocusableWindowState(), that is perfect for toolbars. By the way, I don't know if my previous setFocusable(false) had any effect.

The next issue was that the focus gets very weird behaviour with this code : If I switch from MyApp to Firefox, here is what happens :

focus : MyApp -> Firefox
execution of MyDialog.toFront()
focus : Firefox -> MyDialog
MyDialog not focusable !
focus : MyDialog -> MyApp !!!

result : nothing changed !

So I finally got the tricks : just after MyDialog.toFront(), you give back the focus to the previous owner. And the only way I found to do this with no error was : mainFrame.toBack()

Final code :

public class Test {
    private static JFrame mainFrame;

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                mainFrame = new JFrame("test");
                mainFrame.setSize(800,600);
                mainFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
                mainFrame.setVisible(true);

                A a = new A();
            }
        });
    }

    public static class A extends JDialog {

        public A() {
            super(mainFrame);
            setAlwaysOnTop(true);
            setFocusableWindowState(false);
            setSize(80,60);
            setVisible(true);

            mainFrame.addWindowListener(new WindowAdapter() {
                @Override
                public void windowActivated(WindowEvent e) {
                    A.this.setAlwaysOnTop(true);
                    A.this.toFront();
                }
                @Override
                public void windowDeactivated(WindowEvent e) {
                    A.this.setAlwaysOnTop(false);
                }
            });
        }
    }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!