Search code examples
javaswingfullscreenjfilechooser

JFileChooser in front of fullscreen Swing application


I know there are some topics relative to this question (mainly this unanswered one and this one which is not handling full screen app).

I basically tried every combination of first topic sample and available methods (requestFocus, requestFocusInWindow, ...) but JFileChooser is always displaying behind the fullscreen app. I tried to change filechooser's parent too (setting it to null, itself or the parent frame) with no more success.

Have anyone a working example of this not-that-much-particular use case? Or is there a workaround to let user select files in a fullscreen app?


Solution

  • Unfortunately I can't say how you realised the implementation of the fullscreen app. But I tried a few things and came up with this:

    import java.awt.Color;
    import java.awt.Frame;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    
    import javax.swing.JButton;
    import javax.swing.JFileChooser;
    import javax.swing.JFrame;
    import javax.swing.SwingUtilities;
    
    public class Gui extends JFrame {
    
        public Gui() {
    
            this.setDefaultCloseOperation(EXIT_ON_CLOSE);
    
            //this.setSize(java.awt.Toolkit.getDefaultToolkit().getScreenSize());
            // Set some charateristics of the frame
            this.setExtendedState(Frame.MAXIMIZED_BOTH);
            this.setBackground(Color.black);
            this.setUndecorated(true);
    
            JButton a = new JButton("PRESS ME!");
    
            a.addActionListener(new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent arg0) {
                    JFileChooser fc = new JFileChooser();
                    fc.showOpenDialog(getParent());
                }
            });
    
            this.add(a);
    
            this.setVisible(true);
    
        }
    
        public static void main(String[] args) {
            SwingUtilities.invokeLater(new Runnable() {
    
                @Override
                public void run() {
                    new Gui();
                }
            });
        }
    }
    

    Pay attention to the fact, that I created a new JFileChooser with the parent of the current JFrame as parameter.

    EDIT: I now even tried to set

    java.awt.GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().setFullScreenWindow(new Gui());
    

    and without the

    this.setUndecorated(true);
    

    it worked for me (got a nice fullscreen view and the JFileChooser was in the front). I believe the problem with the window decoration is linked to my window manager (I'm using linux with gnome).

    Hopefully this solution works for you, if not: Could you explain a little bit more, how you create the fullscreen app?