Java:强制 JFileChooser 到一个目录及其子文件夹

Java: Force JFileChooser to one directory and it's sub-folders

我创建了一个 JFileChooser,我想 限制 它只在 user.home 目录及其 子文件夹。

我的 JFileChooser 的 selection 模式仅 目录。

到目前为止我用过这个:

//JButton select = new JButton();
final File directorylock = new File(System.getProperty("user.home"));
JFileChooser browse = new JFileChooser(directorylock);
browse.setFileView(new FileView() {
    @Override
    public Boolean isTraversable(File f) {
         return directorylock.equals(f);
    }
});

但每次我打开 JFileChooser 时,它只显示 user.home 目录 而没有 它的子文件夹,因此我无法访问它们或 select 它们。

它应该如何工作: 打开 JFileChooser 并显示 user.home 目录及其所有子文件夹。能够访问子文件夹和 select 它们。 不能 能够访问父文件夹。 user.home 目录。

我希望这里有人知道应该怎么做! :) 提前谢谢你们 :D

请参考此示例,它可以正常工作,如您所愿,

import java.awt.*;
import java.awt.event.*;
import java.io.File;
import javax.swing.*;
import javax.swing.filechooser.FileView;

public class JFileChooserExample {

       private JFrame mainFrame;
       private JLabel headerLabel;
       private JLabel statusLabel;
       private JPanel controlPanel;

       public JFileChooserExample(){
          prepareGUI();
       }

       public static void main(String[] args){
           JFileChooserExample  swingControlDemo = new JFileChooserExample();      
          swingControlDemo.showFileChooserDemo();
       }

       private void prepareGUI(){
          mainFrame = new JFrame("Java Swing Examples");
          mainFrame.setSize(400,400);
          mainFrame.setLayout(new GridLayout(3, 1));
          mainFrame.addWindowListener(new WindowAdapter() {
             public void windowClosing(WindowEvent windowEvent){
                System.exit(0);
             }        
          });    
          headerLabel = new JLabel("", JLabel.CENTER);        
          statusLabel = new JLabel("",JLabel.CENTER);    

          statusLabel.setSize(350,100);

          controlPanel = new JPanel();
          controlPanel.setLayout(new FlowLayout());

          mainFrame.add(headerLabel);
          mainFrame.add(controlPanel);
          mainFrame.add(statusLabel);
          mainFrame.setVisible(true);  
       }

       private void showFileChooserDemo(){
          headerLabel.setText("Control in action: JFileChooser"); 

          final File directorylock = new File(System.getProperty("user.home"));
          final JFileChooser  fileDialog = new JFileChooser(directorylock);

          fileDialog.setFileView(new FileView() {
                @Override
                public Boolean isTraversable(File f) {
                     return directorylock.equals(f);
                }
            });

          JButton showFileDialogButton = new JButton("Open File");
          showFileDialogButton.addActionListener(new ActionListener() {
             @Override
             public void actionPerformed(ActionEvent e) {
                int returnVal = fileDialog.showOpenDialog(mainFrame);
                if (returnVal == JFileChooser.APPROVE_OPTION) {
                   java.io.File file = fileDialog.getSelectedFile();
                   statusLabel.setText("File Selected :" 
                   + file.getName());
                }
                else{
                   statusLabel.setText("Open command cancelled by user." );           
                }      
             }
          });
          controlPanel.add(showFileDialogButton);
          mainFrame.setVisible(true);  
       }
    }

Out-Put :

稍作修改,我认为 Vishal Gajera 的解决方案可能有效。 我从 Check if file is in (sub)directory 中复制了一个方法,由 tsauerwein

   /**
    * Checks, whether the child directory is a subdirectory of the base 
    * directory.
    *
    * @param base the base directory.
    * @param child the suspected child directory.
    * @return true, if the child is a subdirectory of the base directory.
    * @throws IOException if an IOError occured during the test.
    */
   public boolean isSubDirectory(File base, File child) {

       boolean res = false;

       try {
        base = base.getCanonicalFile();
           child = child.getCanonicalFile();

           File parentFile = child;
           while (!res && parentFile != null) {
               if (base.equals(parentFile)) {
                   res = true;
               }
               parentFile = parentFile.getParentFile();
           }
       } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
       }

       return res;
   }


 private void showFileChooserDemo(){
      headerLabel.setText("Control in action: JFileChooser"); 

      final File directorylock = new File(System.getProperty("user.home"));
      final JFileChooser  fileDialog = new JFileChooser(directorylock);

      fileDialog.setFileView(new FileView() {
            @Override
            public Boolean isTraversable(File f) {
                 return isSubDirectory(directorylock, f);
            }
        });

      JButton showFileDialogButton = new JButton("Open File");
      showFileDialogButton.addActionListener(new ActionListener() {
         @Override
         public void actionPerformed(ActionEvent e) {
            int returnVal = fileDialog.showOpenDialog(mainFrame);
            if (returnVal == JFileChooser.APPROVE_OPTION) {
               java.io.File file = fileDialog.getSelectedFile();
               statusLabel.setText("File Selected :" 
               + file.getName());
            }
            else{
               statusLabel.setText("Open command cancelled by user." );           
            }      
         }
      });
      controlPanel.add(showFileDialogButton);
      mainFrame.setVisible(true);  
   }

查看 Single Root File Chooser

您只会看到您在组合框中指定的单个文件,这样您就不会混淆您可以 select 父目录的用户。

那么您将只能select创建class时指定的目录或文件子目录中的文件。