I would like to activate my Swing application programmatically. I want to say that I would like to write code that makes me JFramevisible and focused (the window title should be highlighted). I tried to use requestFocus(). It works only if the application has at least 2 windows A and B: A is hidden, B is visible. Now, if I call A.requestFocus(), it becomes active as I want. This does not happen if the application has only one window or if both windows are invisible.
I found 2 workarounds.
- use a fake transparent unscreened frame that is always on top. This fake window will play the role of window B. I have not tried to implement it, but it seems that it should work.
- a challenge
A.setAlwaysOnTop(true). This brings window A on top of other windows. But that is not the focus. Use java.awt.Robot(mouseMove, mousePress, mouseRelease) to click the title bar of window A. Now call A.setAlwaysOnTop(false)and move the mouse cursor back to the previous position. I implemented the code and it works, but it looks like an ugly workaround.
Is there a “right” solution?
Alexr source
share