3
import java.awt.DisplayMode;
import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
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.JPanel;

public class Sample {
    public static String audioName;
    /**
     * @param args the command line arguments
     */
    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));
        frame.setVisible(true);

        JButton btn = new JButton();
        btn.setText("Button");
        JPanel panel = new JPanel();

        panel.add(btn);
        frame.add(panel);
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                int returnName = chooser.showOpenDialog(frame);
                if (returnName == JFileChooser.APPROVE_OPTION) {
                    System.out.println("Sample");
                }
            }
        });
    }
}

How can I show the JFileChooser inside my full screen? I'm not familiar with JInternalFrame/JDesktopPane, do you think that will fix this problem or is there another method of doing this?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Jong
  • 131
  • 1
  • 2
  • 12
  • If a Java application changes my Windows display settings, I will never use that application again. – Gilbert Le Blanc Dec 26 '12 at 18:15
  • @GilbertLeBlanc, java only changes it temporary while running the application. – Jong Dec 26 '12 at 18:17
  • @GilbertLeBlanc Might have noticed the same effect that irritates me so much about this code. Every time I run it, it forces 2 screens of WinAmp from the far right of my screen where they normally are, into the constraints of the new screen size. And this brings me to the fact that it seems that the full-screen mode is catching you out at every step. What is so darned important about this app. that it should ever be 'full-screen'? – Andrew Thompson Dec 26 '12 at 23:31
  • @Andrew Thompson: I don't have a problem with full screen Java apps. I do have a problem with someone changing my display settings. On further review, if anyone in my organization wrote a Java app that changed my display resolution, I would go and beat the crap out of them until I was shot by the security guard or thrown out of the building. – Gilbert Le Blanc Dec 27 '12 at 00:40
  • *"I don't have a problem with full screen Java apps."* The OP is. Look at the history of their questions. – Andrew Thompson Dec 27 '12 at 00:46
  • If Nick Rippe answer does not fit your need, [see ROT13 answer on my same-issue question.](http://stackoverflow.com/questions/21785837/jfilechooser-in-front-of-fullscreen-swing-application) – bagage Feb 20 '14 at 13:33

2 Answers2

3

The JFileChooser is in the center of the frame for me on a Windows XP computer with Java 6. I moved the frame to various places on my two displays.

I commented out the lines that change the display settings, and fixed a few other problems.

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.JPanel;
import javax.swing.SwingUtilities;

public class Sample implements Runnable {
    public static String    audioName;

    public void run() {
        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));

        JButton btn = new JButton();
        btn.setText("Button");
        JPanel panel = new JPanel();

        panel.add(btn);
        frame.add(panel);
        frame.setExtendedState(
                frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
        btn.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                JFileChooser chooser = new JFileChooser();
                int returnName = chooser.showOpenDialog(frame);
                if (returnName == JFileChooser.APPROVE_OPTION) {
                    System.out.println("Sample");
                }
            }
        });
        frame.setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Sample());
    }
}

If you want to maximize your JFrame, you add the following statement somewhere before your setVisible method.

frame.setExtendedState(frame.getExtendedState() | JFrame.MAXIMIZED_BOTH);
Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111
0

I would suggest instead of using using a Popup, just embed the JFileChooser into your application. It'll make your code a little longer, but from my perspective 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