Selecting Folder Destination in Java?

前端 未结 5 1465
臣服心动
臣服心动 2020-12-05 00:04

I am a newbie to Java. I am trying to dynamically choose the file location to save the outcome of my project (to be initiated at the very start of my project). I worked arou

5条回答
  •  温柔的废话
    2020-12-05 00:12

    I found a good example of what you need in this link.

    import javax.swing.JFileChooser;
    
    public class Main {
      public static void main(String s[]) {
        JFileChooser chooser = new JFileChooser();
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle("choosertitle");
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        chooser.setAcceptAllFileFilterUsed(false);
    
        if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
          System.out.println("getCurrentDirectory(): " + chooser.getCurrentDirectory());
          System.out.println("getSelectedFile() : " + chooser.getSelectedFile());
        } else {
          System.out.println("No Selection ");
        }
      }
    }
    

提交回复
热议问题