禁用文件选择器中的新文件夹按钮不能正常工作

Cyp*_*rUS 2 java swing jfilechooser

我使用以下代码禁用新的"文件夹"按钮:

 public void disableNewFolderButton( Container c ) {

     System.out.print("in disable fn");
int len = c.getComponentCount();
for (int i = 0; i < len; i++) {
  Component comp = c.getComponent(i);
  if (comp instanceof JButton) {
    JButton b = (JButton)comp;
    Icon icon = b.getIcon();
    if (icon != null
         && icon == UIManager.getIcon("FileChooser.newFolderIcon"))
    {
        System.out.print("in disable fn");
       b.setEnabled(false);
    }
    }
  else if (comp instanceof Container) {
    disableNewFolderButton((Container)comp);
  }
}
 }
Run Code Online (Sandbox Code Playgroud)

代码在以下行中调用:

   JFileChooser of=new JFileChooser();
    of.setAcceptAllFileFilterUsed(false);
    of.addChoosableFileFilter(new MyFilter());
    disableNewFolderButton(of);
Run Code Online (Sandbox Code Playgroud)

但是,仅在首次显示文件选择器时才会禁用新文件夹按钮.假设我去了一些驱动器,比如说g :,那么按钮再次启用.如何设置这个权利?

Tor*_*res 6

这对我有用......

    //Create a file chooser
UIManager.put("FileChooser.readOnly", Boolean.TRUE); 
JFileChooser fc = new JFileChooser();
Run Code Online (Sandbox Code Playgroud)