2

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?

Community
  • 1
  • 1
bagage
  • 1,094
  • 1
  • 21
  • 44
  • Why not `setExtendedState(JFrame.MAXIMIZED_BOTH)` instead? – trashgod Feb 14 '14 at 19:12
  • Because maximized window is not fullscreen window - the Windows task bar is sitll there and I do not want it – bagage Feb 16 '14 at 16:08
  • I rarely use full screen and never noticed this effect. You might edit your question to clarify the requirement, identify the target platform, and include a [*Minimal, Complete, Tested and Readable Example*](http://stackoverflow.com/help/mcve) that shows your current approach. – trashgod Feb 16 '14 at 20:20

3 Answers3

3

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?

ROT13
  • 375
  • 2
  • 7
  • Thanks for the example (note: given code is missing `import java.awt.Frame;`). Unfortunately this is not working on CentOS, [see picture](http://postimg.org/image/p8wljn0gr/786a51ec/). I will try it on Windows this afternoon but I think this is not gonna working. I will edit my question. – bagage Feb 17 '14 at 13:17
  • Actually your code is working on Windows 8, but not working on CentOS (previous comment) and not fully working on gnome-shell, [see the second picture](http://s11.postimg.org/h3m2zndn7/aaaa.png). But with your edit this is working on gnome-shell too. – bagage Feb 17 '14 at 17:12
  • Jep, i also recognized the issues with gnome... Therefore the edit ;). I don't know enought about CentOS to know how to fix the problem. Did you find a solution yourself? I found a Problem with a bug concerning fullscreen apps and Java 6. Maybe this helps [link](http://stackoverflow.com/questions/8837719/java-in-full-screen-on-linux-how-to-cover-task-bar) – ROT13 Feb 17 '14 at 19:47
  • Actually I did not test the EDIT solution on the centOS machine; I will try it tomorrow. Even if it does not work, your solution is fine since centOS is not one of my OS supported targets. – bagage Feb 17 '14 at 19:49
  • I'm glad to hear this. Maybe you could drop a comment after trying on the CentOS. I'm just curious ;). – ROT13 Feb 17 '14 at 19:51
0

I would suggest instead of using using a Popup, just embed the JFileChooser into your application. It doesn't really make sense to have popups in a windowless application (Personally, I don't like popups much anyways).

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class FullScreenApp {

    public static void main(String[] args) {
        final JFrame frame = new JFrame();
        frame.setTitle("Frame");
        frame.setSize(800, 600);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        GraphicsDevice device = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        device.setFullScreenWindow(frame);
        device.setDisplayMode(new DisplayMode(800, 600, 32, 60)); // Ugh.
        frame.setVisible(true);

        final Box panel = Box.createVerticalBox();
        JButton btn = new JButton();
        btn.setText("Button");

        panel.add(btn);
        frame.add(panel);

        final CustomFileChooser chooser = new CustomFileChooser(panel);

        btn.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
               chooser.show();
            }
        });
    }

    public static class CustomFileChooser extends JFileChooser{
         /** Node this chooser should be added to.
          *  There's likely a better way of doing this, 
          *  but it was convenient for a quick example */
        Container parent;

        public CustomFileChooser(Container parent){
            super();
            this.parent = parent;
            //Make configurations for your file chooser
            setApproveButtonText("Open");
        }

        @Override
        public void approveSelection(){
            super.approveSelection();
            //Perform accept action here
            System.out.println(getSelectedFile().getAbsolutePath());
            parent.remove(CustomFileChooser.this);
            parent.repaint();
        }

        @Override
        public void cancelSelection(){
            super.cancelSelection();
            //Perform cancel action here
            System.out.println("Canceled");
            parent.remove(CustomFileChooser.this);
            parent.repaint();
        }

        @Override
        public void show(){
             rescanCurrentDirectory();
             parent.add(this);
             revalidate();
             repaint();
        }

        @Override
        public Dimension getMaximumSize(){
            //Not necessary - But I felt the chooser should have a maximum size
            return new Dimension(500,300);
        }
    }
}
Nick Rippe
  • 6,465
  • 14
  • 30
0

FullscreenLib

    //import
    import argha.util.Fullscreen;

    //this for JFrame
    //true for setting Undecorated on/off
    Fullscreen screen = new Fullscreen(this, true);
    screen.DoTheWorkFor();

You can use my library for creating fullscreen windows and the problem you are facing hope it solved after that i tested and its working.

Hope it may helped you

Argha Das
  • 13
  • 7