8

在 JFileChooser 的 Windows 外观中,JFileChooser 对话框的左侧显示了五个按钮:Recent Items、Desktop、My Documents、Computer 和 Network。这些每个都代表文件系统的视图,就像 Windows 资源管理器将显示的那样。除非调用 setSelectedFile() 或 setCurrentDirectory() 方法,否则 JFileChooser 似乎默认为 My Documents View。

我试图让用户可以轻松地选择多个映射网络驱动器之一,这些驱动器应该出现在“计算机”视图中。有没有办法将 JFileChooser 设置为默认打开“计算机”视图?

我尝试了几种方法来强制它,最近的是找到根目录并将其设置为 currentDirectory,但这显示了该根节点的内容。最新的代码包含在下面。

private File originalServerRoot;
private class SelectOriginalUnitServerDriveListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JFileChooser origDriveChooser = new JFileChooser();
            origDriveChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            File startFile = new File(System.getProperty("user.dir")); //Get the current directory

            // Find System Root
            while (!FileSystemView.getFileSystemView().isFileSystemRoot(startFile))
            {
                startFile = startFile.getParentFile();
            }

            origDriveChooser.setCurrentDirectory(startFile);
            origDriveChooser.setDialogTitle("Select the Mapped Network Drive");
            int origDriveChooserRetVal = origDriveChooser.showDialog(contentPane,"Open");
            if (origDriveChooserRetVal == JFileChooser.APPROVE_OPTION)
            {
                originalUnitServerRoot = origDriveChooser.getSelectedFile();

            }
        }
    }

有没有一种方法可以让我默认选择“计算机”视图(或网络,或任何其他视图),或者以任何方式欺骗 JFileChooser?

编辑
感谢您提供快速而彻底的答案。我结合了 Hovercraft Full Of Eels 和 Guillaume Polet 的答案,试图让代码在任何驱动器号上工作。结果代码如下。再一次,谢谢。

private File originalServerRoot;
private class SelectOriginalUnitServerDriveListener implements ActionListener
    {
        @Override
        public void actionPerformed(ActionEvent e)
        {
            JFileChooser origDriveChooser = new JFileChooser();
            origDriveChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
            File startFile = new File(System.getProperty("user.dir")); //Get the current directory

            // Find System Root
            while (!FileSystemView.getFileSystemView().isFileSystemRoot(startFile))
            {
                startFile = startFile.getParentFile();
            }
            //Changed the next line
            origDriveChooser.setCurrentDirectory(origDriveChooser.getFileSystemView().getParentDirectory(rootFile));
            origDriveChooser.setDialogTitle("Select the Mapped Network Drive");
            int origDriveChooserRetVal = origDriveChooser.showDialog(contentPane,"Open");
            if (origDriveChooserRetVal == JFileChooser.APPROVE_OPTION)
            {
                originalUnitServerRoot = origDriveChooser.getSelectedFile();

            }
        }
    }
4

3 回答 3

7

这是一个工作示例。它假设 C:\ 是有效路径。它使用FileSystemView.getParentDir(File)

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {

            @Override
            public void run() {
                new Test().initUI();
            }
        });
    }

    protected void initUI() {
        JFrame frame = new JFrame();
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        JPanel panel = new JPanel();
        final JButton button = new JButton("Select files...");
        button.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                final JFileChooser chooser = new JFileChooser();
                chooser.setCurrentDirectory(
                                 chooser.getFileSystemView().getParentDirectory(
                                     new File("C:\\")));  
                            chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
                chooser.showDialog(button, "Select file");
            }
        });
        panel.add(button);
        frame.add(panel);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }
}
于 2012-05-09T22:01:22.733 回答
4

这样做的一种方法是获取默认目录的父目录,直到toString()获得的文件是“计算机”。就像是:

  FileSystemView fsv = FileSystemView.getFileSystemView();
  File defaultFile = fsv.getDefaultDirectory();
  while (defaultFile != null) {
     defaultFile = defaultFile.getParentFile();
     if (defaultFile != null && "Computer".equalsIgnoreCase(defaultFile.toString())) {
        JFileChooser fileChooser = new JFileChooser(defaultFile);
        fileChooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
        int result = fileChooser.showOpenDialog(null);
        if (result == JFileChooser.APPROVE_OPTION) {
           File file = fileChooser.getSelectedFile();
           System.out.println(file);
        }
     }
  }
于 2012-05-09T21:57:06.173 回答
0
//Specify the absolute path of the Mapped Drive

chooser.setCurrentDirectory(new File("B:\\exampleFolder"));

或者

// set the file opener to look at the desktop    

JFileChooser chooser = new JFileChooser();        
chooser.setCurrentDirectory(new File(System.getProperty("user.home") + "\\Desktop"));
于 2014-09-26T20:52:07.150 回答