如何使JFileChooser默认为计算机视图而不是我的文档

时间:2012-05-09 21:31:08

标签: java windows swing jfilechooser jdialog

在JFileChooser的Windows外观中,JFileChooser对话框的左侧显示五个按钮:Recent Items,Desktop,My Documents,Computer和Network。这些都表示文件系统的视图,因为Windows资源管理器会显示它们。除非调用setSelectedFile()或setCurrentDirectory()方法,否则JFileChooser默认为My Documents视图。

我试图让用户轻松选择多个映射网络驱动器中的一个,这些驱动器应出现在“计算机”视图中。有没有办法设置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();

            }
        }
    }

3 个答案:

答案 0 :(得分: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);
    }
}

答案 1 :(得分:4)

执行此操作的kludge方法是获取默认目录的父级,直到获得的文件的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);
        }
     }
  }

答案 2 :(得分:0)

//Specify the absolute path of the Mapped Drive

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

OR

// set the file opener to look at the desktop    

JFileChooser chooser = new JFileChooser();        
chooser.setCurrentDirectory(new File(System.getProperty("user.home") + "\\Desktop"));