0

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.

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);
                    }
              }
        });
amal
  • 3,470
  • 10
  • 29
  • 43
  • @vishal_aim I set the enable false at the form load. Then at the end of the validation method set the enable true for Save button. Then i called Validate method at last text field's pressed event. – amal Apr 21 '13 at 12:20
  • What if user filled the last text field and clear one of the previous text fields?? Then you need to add that action to all input elements. – Praneeth Peiris Apr 21 '13 at 12:23
  • @GnomezGrave exactly. Thats the problem im having here. Im gonna try what you suggested. – amal Apr 21 '13 at 12:27

3 Answers3

1

did you try like this?

    final JTextField textField = new JTextField();
    final JButton submitBtn = new JButton();
    submitBtn.setEnabled(true);

    textField.getDocument().addDocumentListener(new DocumentListener() {
        public void changedUpdate(DocumentEvent e) {
            validate(e);
        }
        public void removeUpdate(DocumentEvent e) {
            validate(e);
        }
        public void insertUpdate(DocumentEvent e) {
            validate(e);
        }

        public void validate(String enteredText) {
            String currText = "";
            try {
                Document doc = (Document)e.getDocument();
                currText = doc.getText(0, doc.getLength());
            } catch (BadLocationException e1) {
                e1.printStackTrace();
            }
            //validation of currText here

            //if validated successfully
            submitBtn.setEnabled(true);
            //else
            submitBtn.setEnabled(false);
        }
    });
vishal_aim
  • 7,636
  • 1
  • 20
  • 23
  • Thnx for your reply. No. I havent tried something like this. I ll try this out and let you know. – amal Apr 21 '13 at 12:38
  • good idea but with, I don't like that, [please to see proper (my view), because String could be inserted from ClipBoard, or previously selected text can be deleted too](http://stackoverflow.com/questions/8572948/java-jformattedtextfield-saving-numbers/8582656#8582656) – mKorbel Apr 21 '13 at 12:44
  • @vishal_aim I tried using the code you gave me. But it didnt work for me. Sorry Im still java beginner. Could you look at my code pls. I updated the original post – amal Apr 21 '13 at 13:30
  • Thanks, updated code. @user2033382, you can try this like now, Plain text components do not fire changedUpdate events . – vishal_aim Apr 22 '13 at 04:59
  • @vishal_aim Thnx for your update. I understand I have to work around documentlistner to get my expected result. Im trying this for the first time. I edited my previous code according to your updated code. But still no luck. :( I updated original post. Could you look at where I have messed up. – amal Apr 22 '13 at 06:32
  • its working for me: `String currText = ""; try { Document doc = (Document)e.getDocument(); currText = doc.getText(0, doc.getLength()); } catch (BadLocationException e1) { e1.printStackTrace(); } if(currText.isEmpty()) btn.setEnabled(false); else btn.setEnabled(true);` dont use txt1.getText() – vishal_aim Apr 22 '13 at 07:02
  • @vishal_aim Wow. Your correct. Its working in that way. Thnx Lot. Voted up and set as answer already. Just one more thing, How do i do this for multiple textfields. I mean set Save button enable only if all textfields not empty, Unless make it disable. – amal Apr 22 '13 at 07:08
1

Condition the enabled property of your Save button using setEnabled() in two places:

  • In your implementation of shouldYieldFocus() in an InputVerifier attached to each relevant component. The tutorial and some examples are cited here.

  • In your component's normal listener.

Community
  • 1
  • 1
trashgod
  • 203,806
  • 29
  • 246
  • 1,045
0

Create a method to check if all the inputs are completed or/and all the validations are passed and finally return a boolean.

public boolean validate(...){
   //some stuff
   if(validated){
     return true;
   }else{
     return false;
   }
}

then you can use it like.

button.setEnabled(validate(...));
Praneeth Peiris
  • 2,008
  • 20
  • 40
  • Thanx for your response. I already tried something like this. But didnt try like this. button.setEnabled(validate(...)); Let you know soon – amal Apr 21 '13 at 12:25