禁止在JFileChooser中重命名文件?

kof*_*cii 5 java jfilechooser

在JFileChooser中对文件单击两次(而不是双击)时,可以重命名所选文件.如何禁用此功能?我试过了

UIManager.put("FileChooser.readOnly", Boolean.TRUE);
Run Code Online (Sandbox Code Playgroud)

但它不起作用.

pau*_*sm4 11

令人惊讶的是,您无法禁用重命名文件/从JFileChooser本身创建新目录.正如您所推测的那样,您需要从UIManager中禁用此FileChooser"功能".

这是一个可能有用的片段:

http://www.coderanch.com/t/555535/GUI/java/FileChooser-readOnly

  Boolean old = UIManager.getBoolean("FileChooser.readOnly");  
  UIManager.put("FileChooser.readOnly", Boolean.TRUE);  
  JFileChooser fc = new JFileChooser(".");  
  UIManager.put("FileChooser.readOnly", old);  
Run Code Online (Sandbox Code Playgroud)

关键是在创建文件选择器之前设置"FileChooser.readOnly".

  • @mre是的,`UIManager.put("FileChooser.readOnly",Boolean.TRUE);`对我有用,是的``new folder`选项也没了. (2认同)
  • 您可以通过JFileChooser禁用创建文件.只需使用带有javax.swing.filechooser.FileSystemView扩展的JFileChooser(FileSystemView)构造函数,其中包含createFileObject(...)和createNewFolder(...)的空实现.这也可以用来阻止用户浏览其他位置等. (2认同)