title: Disabling the JFileChooser approve button
body: <p>I have created an UI for Opening a project folder using JFileChooser. When user opening the project folder, the Open dialog button want to disable until the required folder selected.</p>
  <pre><code>//This is my button action for opening JFileChooser
  
  private void btnOpenFileActionPerformed(java.awt.event.ActionEvent evt) {                                            
          // TODO add your handling code here:
  
          try {
              UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
              //Create an object for ProjectChooser
              ProjectChooser projectChooser = new ProjectChooser();
  
              //OPen the JFileChooser window
              projectChooser.setDialogTitle(&quot;Open Test Project&quot;);
  
              projectChooser.setAcceptAllFileFilterUsed(false);
              projectChooser.setFileFilter(new javax.swing.filechooser.FileFilter() {
                  @Override
                  public boolean accept(File f) {
                      return f.isDirectory() || f.isFile();
                  }
  
                  @Override
                  public String getDescription() {
                      return &quot;Test Project&quot;;
                  }
              });
  
              int res = projectChooser.showOpenDialog(null);
              if (res == JFileChooser.APPROVE_OPTION) {
  
                  title.setText(projectChooser.getSelectedFile().getAbsolutePath() + &quot; - &quot; + &quot;ZUI&quot;);
                  dirPath.setText(projectChooser.getSelectedFile().getAbsolutePath());
                  dirPath.setVisible(false);
                  output.setText(projectChooser.getSelectedFile().getAbsolutePath() + &quot;\nrun: \n&quot;);
             
              }        
          } catch (Exception ex) {
              ex.printStackTrace();
          }
  
  </code></pre>
  <p>//This is my ProjectChooser.java File
  i found this solution from <a href="https://stackoverflow.com/questions/14061450/disable-open-button-in-jfilechooser">Disable Open button in JFileChooser?</a></p>
  <pre><code>public class ProjectChooser extends JFileChooser {
  
      private static JButton approveButton, cancelButton;
  
      public ProjectChooser() {
  //Handle jFileChooser's approve Button
          if (approveButton == null) {
              approveButton = lookupButton(ProjectChooser.this, getUI().getApproveButtonText(this));
              approveButton.setEnabled(false);
          }
  
          setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
  
          PropertyChangeListener propertyChangeListener = new PropertyChangeListener() {
              @Override
              public void propertyChange(PropertyChangeEvent evt) {
                  if (evt.getPropertyName().equals(JFileChooser.SELECTED_FILE_CHANGED_PROPERTY)) {
  
                      // System.out.println(evt.getNewValue());
                      String newFile = evt.getNewValue().toString();
                      // System.out.println(newFile);
                      String dif = newFile + &quot;/diffs&quot;;
                      File difFile = new File(dif);
  
                      String scr = newFile + &quot;/screenshots&quot;;
                      File scrFile = new File(scr);
  
                      String testCase = newFile + &quot;/testCases.csv&quot;;
                      File testFile = new File(testCase);
  
                      
                      if (difFile.isDirectory() &amp;&amp; difFile.getName().contains(&quot;diffs&quot;)
                              &amp;&amp; scrFile.getName().contains(&quot;screenshots&quot;)
                              &amp;&amp; testFile.getName().contains(&quot;testCases.csv&quot;)) {
  
                          approveButton.setEnabled(true);
                      } else {
  
                          approveButton.setEnabled(false);
                      }
                  }
              }
          };
          addPropertyChangeListener(SELECTED_FILE_CHANGED_PROPERTY, propertyChangeListener);
      }
  
      private JButton lookupButton(Container c, String text) {
          JButton temp = null;
          for (Component comp : c.getComponents()) {
              if (comp == null) {
                  continue;
              }
              if (comp instanceof JButton &amp;&amp; (temp = (JButton) comp).getText() != null &amp;&amp; temp.getText().equals(text)) {
                  return temp;
              } else if (comp instanceof Container) {
                  if ((temp = lookupButton((Container) comp, text)) != null) {
                      return temp;
                  }
              }
          }
          return temp;
      }
  }
  
  </code></pre>
  <p>My problem is this code works only once when click the btnOpenFile button means, The open dialog button disabled only once. after when I click the btnOpenFile button again the open dialog button is not disabled.
  How can I disable the open dialog button when user select the correct project folder every time. After selecting the correct folder I want to enable the button.</p>
  <p>Thanks.</p>