简体   繁体   中英

JFileChooser not showing

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

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.

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");
              }

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM