title: Enable Jbutton only if Validation is True
body: <p>What is the best way to validate swing application's input fields such as text fields, comboboxes, etc and let the user to press Save button only if everything is ok. Assume that Search function also in the same interface. So searching for record will also fill up input fields. But Save button should remain disable in that case. </p>
  
  <pre><code>initComponents();
          btnSave.setEnabled(false);
  
          txt1.getDocument().addDocumentListener(new DocumentListener() {
                @Override
                public void changedUpdate(DocumentEvent e) {
  
                }
  
                @Override
                public void removeUpdate(DocumentEvent e) {
                      validate(txt1.getText(),e);
                }
  
                @Override
                public void insertUpdate(DocumentEvent e) {
                      validate(txt1.getText(),e);
                }
  
                public void validate(String enteredText,DocumentEvent e) {
                      String currText = "";
                      try {
                            Document doc = (Document) e.getDocument();
                            currText = doc.getText(0, doc.getLength());
                      } catch (BadLocationException e1) {
                      }
  
                      if(enteredText.equals(currText)){
                        //if validated successfully
                             btnSave.setEnabled(false);
                      }else{
                      btnSave.setEnabled(true);
                      }
                }
          });
  </code></pre>