【问题标题】:Java: Force JFileChooser to one directory and it's sub-foldersJava:强制 JFileChooser 到一个目录及其子文件夹
【发布时间】:2016-07-15 18:48:23
【问题描述】:

我创建了一个 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

【问题讨论】:

标签: java swing directory jfilechooser


【解决方案1】:

查看Single Root File Chooser

您只会看到您在组合框中指定的单个文件,因此您不会让用户混淆您可以选择父目录。

那么你将只能在创建类时指定的文件的目录或子目录中选择一个文件。

【讨论】:

  • 很好,但它在页面上说“使用这种方法需要您自担风险”。可能很危险。 ;-P(我在开玩笑)
  • 谢谢你,这个成功了,我修改了他们的代码以满足我的需要,我还必须禁用 JFileChooser 的文本字段,这样超级目录或访问父目录就不可能了。谢谢你:)
【解决方案2】:

请参考此示例,它可以按您的意愿正常工作,

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

输出:

【讨论】:

  • 谢谢你这个例子,是的,我已经测试过了,它有效!唯一的问题(不是您的代码)是我也希望能够访问子目录。只是不是父文件夹。我希望能够访问 user.dir 的每个“子”文件夹。
  • @YassinHajaj 是的,我确定,父母不会被访问,
  • @YassinHajaj 尝试了解我们无法访问父级但能够访问子级...!!
  • 问题是父目录显示在组合框中,所以用户会认为他们可以选择不同的导演。这会使 UI 混乱。
【解决方案3】:

稍作修改,我认为 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);  
   }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-31
    • 2020-09-29
    • 2020-05-16
    • 1970-01-01
    • 2016-03-03
    • 2019-11-24
    • 1970-01-01
    相关资源
    最近更新 更多