Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
326 views
in Technique[技术] by (71.8m points)

java - Frame always on top of my program only

I'm trying to create a kind of toolbar in an undecorated alwaysOnTop frame. Thus, I want my frame to be on top of my main frame, but not on top of frames from other programs. I tried this code :

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

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

    public static class A extends JDialog {

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

But despite the use of JDialog and precising the owner, the frame stay on top of other applications (at least with Ubuntu. Maybe the result is different with other OS ?)

EDIT : Ok, I tried this code for my dialog :

public static class A extends JDialog {

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

        mainFrame.addWindowListener(new WindowAdapter() {
            @Override
            public void windowActivated(WindowEvent e) {
                A.this.setAlwaysOnTop(true);
            }

            @Override
            public void windowDeactivated(WindowEvent e) {
                // A.this.setAlwaysOnTop(false);
                A.this.toBack();
            }
        });
    }
}

The issue now is that when the main window loose focus, the dialog steals the focus back and I don't understand why. For instance, I run my app, I try to switch to Firefox, Firefox appears and covers the mainFrame, but the A dialog gets the focus and stays in the screen. Now, if I select Firefox again, the dialog will at last correctly disappear. Could you explain me why the dialog gets the focus?

Thanks

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...