1

I have a main frame and a kind of toolbar in a JDialog window. I want that "toolbar" to be always on top of MY program only, so I wrote this 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);

                MyDialog d = new MyDialog();
            }
        });
    }

    public static class MyDialog extends JDialog {

        public MyDialog() {
            super(mainFrame);
            setAlwaysOnTop(true);
            setSize(80,60);
            setVisible(true);

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

To summarize, I create a mainFrame, then a JDialog owned by mainFrame. the JDialog will listen to the mainFrame. When mainFrame is desactivated, the dialog is set "toBack". When activated, it is set "alwaysOnTop".

Everything sounds fine, except that when I try to switch from my program to another, the focus seems to go from MyApp to Firefox (for instance), then from Firefox to the JDialog. How can I avoid that ?

Sharcoux
  • 5,546
  • 7
  • 45
  • 78
  • Do you want the `JDialog` to remain on top of everything no matter what or something else? Does `WindowStateListener` also not able to make thingies right for the situation at hand? – nIcE cOw Jun 24 '14 at 14:58
  • Yes, sorry, I shouldn't have write "setFocusable(false)". It was from another example. Here, I have JDialog that can be focused. Not like [here](http://stackoverflow.com/questions/24361899/frame-always-on-top-of-my-program-only?rq=1) – Sharcoux Jun 24 '14 at 16:28

0 Answers0