Selecting Folder Destination in Java?

前端 未结 5 1453
臣服心动
臣服心动 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:08

    try something like this

    JFileChooser chooser = new JFileChooser();
    chooser.setCurrentDirectory(new java.io.File("."));
    chooser.setDialogTitle("select folder");
    chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
    chooser.setAcceptAllFileFilterUsed(false);
    
    0 讨论(0)
  • 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 ");
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-05 00:23

    You could try something like this (as shown here: Select a Directory with a JFileChooser):

    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.util.*;
    
    
    public class DemoJFileChooser extends JPanel
       implements ActionListener {
       JButton go;
    
       JFileChooser chooser;
       String choosertitle;
    
      public DemoJFileChooser() {
        go = new JButton("Do it");
        go.addActionListener(this);
        add(go);
       }
    
      public void actionPerformed(ActionEvent e) {            
        chooser = new JFileChooser(); 
        chooser.setCurrentDirectory(new java.io.File("."));
        chooser.setDialogTitle(choosertitle);
        chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        //
        // disable the "All files" option.
        //
        chooser.setAcceptAllFileFilterUsed(false);
        //    
        if (chooser.showOpenDialog(this) == JFileChooser.APPROVE_OPTION) { 
          System.out.println("getCurrentDirectory(): " 
             +  chooser.getCurrentDirectory());
          System.out.println("getSelectedFile() : " 
             +  chooser.getSelectedFile());
          }
        else {
          System.out.println("No Selection ");
          }
         }
    
      public Dimension getPreferredSize(){
        return new Dimension(200, 200);
        }
    
      public static void main(String s[]) {
        JFrame frame = new JFrame("");
        DemoJFileChooser panel = new DemoJFileChooser();
        frame.addWindowListener(
          new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
              System.exit(0);
              }
            }
          );
        frame.getContentPane().add(panel,"Center");
        frame.setSize(panel.getPreferredSize());
        frame.setVisible(true);
        }
    }
    
    0 讨论(0)
  • 2020-12-05 00:27

    Along with JFileChooser is possible use this:

    UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
    

    for have a Look and Feel like Windows.

    for others settings, view here: https://docs.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html#available

    0 讨论(0)
  • 2020-12-05 00:28

    Oracles Java Tutorial for File Choosers: http://docs.oracle.com/javase/tutorial/uiswing/components/filechooser.html

    Note getSelectedFile() returns the selected folder, despite the name. getCurrentDirectory() returns the directory of the selected folder.

    import javax.swing.*;
    
    public class Example
    {
        public static void main(String[] args)
        {
            JFileChooser f = new JFileChooser();
            f.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); 
            f.showSaveDialog(null);
    
            System.out.println(f.getCurrentDirectory());
            System.out.println(f.getSelectedFile());
        }      
    }
    
    0 讨论(0)
提交回复
热议问题