Take the 2-minute tour ×

I disable the new Folder button using the following code:

 public void disableNewFolderButton( Container c ) {

     System.out.print("in disable fn");
int len = c.getComponentCount();
  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");
  else if (comp instanceof Container) {

The code is called in the following lines:

   JFileChooser of=new JFileChooser();
    of.addChoosableFileFilter(new MyFilter());

But the new folder button is disabled only when the file chooser is first displayed. Suppose i go to some drive , say g: , then the button is enabled again. How to set this right?

share|improve this question
add comment

3 Answers

up vote 5 down vote accepted

this is working for me...

    //Create a file chooser
UIManager.put("FileChooser.readOnly", Boolean.TRUE); 
JFileChooser fc = new JFileChooser();
share|improve this answer
+1 This one works great for me. Though I would prefer on not involving UIManager. Yet I couldn't find a better one. Plus this solution also hides the 'New folder' in the popup menu. Thanks. I wonder here how to use this solution for just one file chooser? –  Boro Jun 15 '11 at 12:58
add comment

Disable the "new folder" Action (which in turn will disable the button):

import java.awt.*;
import javax.swing.*;
import javax.swing.plaf.basic.*;

public class FileChooserAction
    public static void createAndShowUI()
        JFileChooser chooser = new JFileChooser();

        BasicFileChooserUI ui = (BasicFileChooserUI)chooser.getUI();
        Action folder = ui.getNewFolderAction();


    public static void main(String[] args)
        EventQueue.invokeLater(new Runnable()
            public void run()
share|improve this answer
weird but this example doesn't work for me and 'logically' it should. Do you have any suggestions as to reasons why is it this way? –  Boro Jun 15 '11 at 12:55
@Boro, I use JDK6_07 on XP. The posted code work for me with the Metal and Windows LAF's. I'm not sure why it doesn't work for you, it must be a platform/LAF issue. –  camickr Jun 15 '11 at 19:35
Thank you for your answer. You are completely right, it is dependent, in this case JDK dependent. It is all fine when I run 6.0_11 but not when 6.0_25 (my current JDK). –  Boro Jun 16 '11 at 9:44
appreciated your input and solution +1. I wonder if they will sort out this issue in newer JDK. –  Boro Jun 16 '11 at 10:05
add comment

1) It is a bit stupid, but you can keep disabling it in an another Thread. Until the file chooser got invisible.
2) Does hiding the button work? b.setVisible(false);

share|improve this answer
Not the most elegant solution, but works fine. Thanks Martjin –  CyprUS Apr 6 '11 at 15:51
add comment

Your Answer


