Fullscreen Swing frames not focusing correctly in Linux (Windows is fine)

frame, java, linux, swing

Solution

target.requestFocus();

From the JavaDocs:

Note that the use of this method is discouraged because its behavior is platform dependent. Instead we recommend the use of `requestFocusInWindow()`. If you would like more information on focus, see How to Use the Focus Subsystem, a section in The Java Tutorial.

Problem

I've got an application which spawns several fullscreen-no-decoration frames (basically controlling all screen space). My problem, is some buttons on certain frames are designed to "switch screens", which basically means showing another frame instead of the current one. I have achieved this easily in Windows using this: ``` target.setVisible(true); target.requestFocus(); this.parent.setVisible(false); ``` Where `target` is the frame I'm switching to. This works because initially, I set all frames to not visible except for the first "main" frame. Now, when I port this into a Linux environment, I get an ugly "flashing" when changing frames. In this split second, I can see my desktop background and any open windows I have that exist behind my application. I had this issue in Windows before and fixed it by focusing the target frame before making the old one invisible. Any ideas on how to solve this Linux specific issue? edit: ``` setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE); setUndecorated(true); Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize(); setBounds(0,0,screenSize.width, screenSize.height); this.getContentPane().setLayout(null); setVisible(true); validate(); ```

Original source

Related problems