How to disable File Name Input in JFileChooser in Java

JFileChooser is a very powerful Component in Java. It even enables filtering of file names. However, there is no direct method to prevent a user from entering the File Name input in it.

The following method disables the JTextField File input text box in  JFileChooser, by basically taking the JFileChooser as a Component, looking for a Component that matches JTextField and disabling the JTextField input.

public boolean disableTextField(Container container) {
   Component[] comps = container.getComponents();

   for (Component comp : comps) {
      if (cmp instanceof JTextField) {
         ((JTextField) comp).setEnabled(false);
         return true;
      }
      if (comp instanceof Container) {
         if (disableTextField((Container) comp)) return true;
      }
   }
   return false;
}