java:强制jfilechooser到一个目录及其子文件夹

ia2d9nvy  于 2021-07-13  发布在  Java
关注(0)|答案(3)|浏览(379)

我已经创建了一个jfilechooser,我只想将它限制在user.home目录及其子文件夹中。
我的jfilechooser的选择模式是目录。
到目前为止,我已经使用了:

//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目录,没有它的子文件夹,因此我无法访问或选择它们。
工作原理:打开jfilechooser并显示user.home目录及其所有子文件夹。能够访问子文件夹并选择它们。无法访问父文件夹。在user.home目录中。
我希望这里有人知道该怎么做!:)提前谢谢你们:d

uqzxnwby

uqzxnwby1#

查看单根文件选择器。
您只能在组合框中看到指定的单个文件,这样就不会让用户混淆您可以选择父目录。
然后,您将只能在创建类时指定的文件的目录或子目录中选择一个文件。

0h4hbjxa

0h4hbjxa2#

请参考这个样品,你想要什么都行,

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);  
       }
    }

输出:

vatpfxk5

vatpfxk53#

稍加修改,我认为vishal gajera的解决方案可能会奏效。我从check if file in(子)目录复制了一个方法

/**
    * 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);  
   }

相关问题