JFileChooser not showing
Asked Answered
T

3

7

I have had a problem concerning the JFileChooser for a long while now and haven't been able to find help... The problem is that the file window is not showing up. I have tried to find the cause of this problem and I have tested the following:

public class Test {
   public static void main (String[] args) {   
   load();     
   }
   public static void load () {
      String folder = System.getProperty("user.dir");
      JFileChooser fc = new JFileChooser(folder);
      int resultat = fc.showOpenDialog(null);  
   }
}

When running this code I do get the window to show.

But, when I try this:

public class Test {
    public static void main (String[] args) {   
    String input = JOptionPane.showInputDialog(null, "Make your choice!\n" +
                                                     "1. load file");      
    load();   

    }
}

the window is not showing however, the programming is still running... I have no clue what might be causing this problem

Trend answered 8/11, 2015 at 21:3 Comment(5)
If you mean that the JFileChooser doesn't show up until you press OK, Cancel or Exit in the input dialog - Then that's the expected behaviour. The JOptionPane creates a modal dialog that prevents the next line in the program from being called until the dialog closes. But if you don't mean that, I can't reproduce your problem. The program "works" for me :/Funderburk
This is not what I mean, the window is not showing after pressing OK, Cancel or Exit. For some odd reason it seems the JFileChooser does not show when I use a JOptionPane before it...Trend
Swing GUIs should be created and updated on the EDT.Fancied
The example code works fine for me, Windows 7/Java 8Substructure
I'm running Mac OS X Yosemite 10.10.3/ Java 8Trend
C
13

Java on the Mac is really picky about Swing things only occurring in the Event Dispatch Thread. Try this.

import java.awt.EventQueue;
import javax.swing.JFileChooser;

public class Test {
    private static int result;
    public static void main(String[] args) throws Exception {
        EventQueue.invokeAndWait(new Runnable() {
            @Override
            public void run() {
                String folder = System.getProperty("user.dir");
                JFileChooser fc = new JFileChooser(folder);
                result = fc.showOpenDialog(null);
            }
        });
        System.out.println(result);
    }
}

Documentation for InvokeAndWait is here. But basically, you pass it a Runnable that does Swing stuff, and it will execute that in the right thread. There's also InvokeLater if you don't want to wait.

Cosmogony answered 9/11, 2015 at 2:13 Comment(0)
A
0

Here is some code, a JFileChooser needs a child, in this case I use a JDialog

JFileChooser fileChooser = new JFileChooser();
JDialog dialog = new JDialog();  

              fileChooser.setCurrentDirectory(new File(System.getProperty("user.dir")));
              int result = fileChooser.showOpenDialog(dialog);
              if (result == JFileChooser.APPROVE_OPTION) {
                  File selectedFile = fileChooser.getSelectedFile();
                  System.out.println("Selected file: " + selectedFile.getAbsolutePath());
              }else{
                  System.out.println("Cancelled");
              }
Apocarp answered 9/11, 2015 at 0:0 Comment(0)
A
0

I just have a situation, where a JFileChooser should be displayed right away at startup (not on EDT), or after a user input on GUI (therefore on EDT). In this case you cant simply put everything inside a EventQueue.inwokeAndWait() because it will cause an Exception when already running on the EDT. I came up with this solution which was inspired by this Blog entry.

...
private File chosenFile = null;

...
Runnable selectFile = new Runnable() {
  public void run(){
    JFileChooser jfc = new JFileChooser();
    if (jfc.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
      chosenFile = jfc.getSelectedFile();
    }
  }
}
if (SwingUtilities.isEventDispatchThread()) {
  selectFile.run();
}
else {
  SwingUtilities.invokeLater(selectFile);
}
// Now use the private variable chosenFile
...

The drawback of this solution is that you have to define "global" variables to retrieve the result from the JFileChooser. Unless you are running Java 10 or higher, where you could implement the anonymous Runnable, give it some additional features (like a public File chosenFile;) and access it later. See this discussion.

Abeabeam answered 6/7, 2022 at 10:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.