The frame is always on top of my program.

I am trying to create a kind of toolbar in an unecorated 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 using JDialog and limiting the owner, the frame remains on top of other applications (at least with Ubuntu. Maybe the result is different from 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(); } }); } } 

Now the problem is that when the main window expands, the dialog box focuses backward, and I don’t understand why. For example, I launch my application, I try to switch to Firefox, Firefox appears and closes mainFrame, but dialog A focuses and remains on the screen. Now, if I select Firefox again, the dialog will finally disappear. Could you explain to me why the dialogue focuses?

thanks

+8
java swing always-on-top
source share
2 answers

Ok, I found a solution (I don't know if this is a solution, but it works, so ...)

I found setFocusableWindowState (), which is perfect for toolbars. By the way, I do not know if my previous setFocusable (false) had any effect.

The next problem was that the focus gets a very strange behavior with this code: if I switch from MyApp to Firefox, this is what happens:

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

result: nothing has changed!

So, I finally realized: right after MyDialog.toFront () you return focus to the previous owner. And the only way to do this without errors 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); } }); } } } 
+4
source share

You should always make your window on top only when the parent window is activated. Something like that:

 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); } } } 
+4
source share

All Articles