JFileChooser from a command line program and popping up Underneath all windows
Asked Answered
L

3

9

Ive implemented the jFileChooser in my command line program and it works, just as it should with only one annoying issue. It seems that it opens underneath every window with no alert of any kind. In fact I even missed it a couple of times at first leading me to believe that i had implemented it wrong.

I have implemented this as follows:

System.out.println("Please select the file");
JFileChooser fc = new JFileChooser();
int retValue = fc.showOpenDialog(new JPanel());
if(retValue == JFileChooser.APPROVE_OPTION){
    g.inputFile = fc.getSelectedFile();
}else {
    System.out.println("Next time select a file.");
    System.exit(1);
}

Essentially I only want the jFileChooser in order to have the user select a file as an input file. This is the only component that has a need for a GUI implementation, so if i can avoid writing up an GUI, that would be helpful.

Louvre answered 21/9, 2011 at 4:6 Comment(2)
see Edit 2 in my answer below.Raye
This thread may or may not help: #5129794Gotland
L
12

So after trying a variety of things from different stack overflow topics I ended up with a result that consistently and reliably opens above every window on Windows 7.

public class ChooseFile {
    private JFrame frame;
    public ChooseFile() {
        frame = new JFrame();

        frame.setVisible(true);
        BringToFront();
    }
    public File getFile() {
        JFileChooser fc = new JFileChooser();
        if(JFileChooser.APPROVE_OPTION == fc.showOpenDialog(null)){
            frame.setVisible(false);
            return fc.getSelectedFile();
        }else {
            System.out.println("Next time select a file.");
            System.exit(1);
        }
        return null;
    }

    private void BringToFront() {                  
                    frame.setExtendedState(JFrame.ICONIFIED);
            frame.setExtendedState(JFrame.NORMAL);

    }

}

As it stands in my program it is an inner class and is invoked by calling:

System.out.println("Please select the file");
g.inputFile = g.new ChooseFile().getFile();
Louvre answered 24/9, 2011 at 3:8 Comment(3)
This helped me immensely. Just these 5 lines in one chunk right before the use of JFileChooser fixed everything: final JFrame jFrame; jFrame = new JFrame(); jFrame.setVisible(true); jFrame.setExtendedState(JFrame.ICONIFIED); jFrame.setExtendedState(JFrame.NORMAL);Chronogram
I am actually looking for a still simpler solution here (no JFrame): #17439130 ...but it doesn't look like I will get any answers as easy to implement as this one.Chronogram
Thank for the great help! I customized it to my needs!It really works!Labbe
R
4

I think of two possible causes for something like this:

  • You're trying to mix AWT and Swing GUI's in the same program, or
  • You're trying to mix a console program (i.e., using System.out.println(...) and getting input via a Scanner object) with a Swing GUI.

If you are doing either of these two things, then you should simplify and make it only a Swing GUI type program. If this information doesn't help, then you may wish to give us more information about your problem.

Edit 1
I just noticed the details of your code. The new JPanel() part below is a problem:

int retValue = fc.showOpenDialog(new JPanel());

To make the JFileChooser act as a dialog to your top level window (which it is currently not doing and which is your chief problem), you should instead pass a component that is in the parent top level window, such as a JPanel or JButton that is held inside of your JFrame or JApplet.

Edit 2
OK, you're trying to mix a Java console program with a Swing GUI program which is like eating ice cream with pickles -- they just don't go well together. There is no top-level window to offer to the JFileChooser's showOpenDialog method so that it will act as a true dialog.

The best solution is to not do this, to instead re-write your application to be a complete Swing GUI.

Raye answered 21/9, 2011 at 4:17 Comment(0)
E
3

In my code, I can just use null and it works. I'm using Java 7 on Windows 7.

JFileChooser chooser = new JFileChooser(System.getProperty("java.class.path"));
FileNameExtensionFilter filter = new FileNameExtensionFilter("CSV files", "csv");
chooser.setFileFilter(filter);
int returnVal = chooser.showOpenDialog(null);
if(returnVal == JFileChooser.APPROVE_OPTION) {
 try {
  Scanner inputFile= new Scanner(new File(chooser.getSelectedFile().getAbsolutePath()));
 } catch (FileNotFoundException e) {
  e.printStackTrace();
 }
}
Eschatology answered 30/3, 2014 at 11:30 Comment(1)
Nice straight answer without a sermon. Can confirm that the above code works in 1.7 & 1.8.Staging

© 2022 - 2024 — McMap. All rights reserved.