2

I have this code sample to see which Jtextfield is empty. I have attached a image of my application too. All I need to know is that, when a user was not entering the details in a specific jTextfield, and click "Register" button, I want the user to be informed about his mistake/s like;

"You haven't entered Student Middle Name" or "You have not entered Student Address" or "You haven't entered Student middle name and Address"

I want the user to inform SPECIFICALLY which jTextfield/s is/are EMPTY and set its/Their background/s RED and stop saving the details into database until he fills all the JtextFields. I have tried many codes but any of it didn't work :(

Here's my Code. I have used the array to check the Jtextfield/s are empty or not, but I don't know how to inform the user which Jtextfield/s is/are causing the problem. Please Help Me :(

public void checkEmpty() {
    String fname = jTextField1.getText();
    String mname = jTextField2.getText();
    String lname = jTextField3.getText();

    String lineone = jTextField4.getText();
    String linetwo = jTextField5.getText();
    String linethree = jTextField6.getText();

    int fnam = fname.length();
    int mnam = mname.length();
    int lnam = lname.length();
    int lineon = lineone.length();
    int linetw = linetwo.length();
    int linethre = linethree.length();

    int[] check = {fnam, mnam, lnam, lineon, linetw, linethre};
    for (int i = 0; i < check.length; i++) {
        if (check[i] == 0) {
            //needs to show which jTextfield/s is/are empty and make their backgrounds RED
        } else {
            //save to database----> I know what I have to do here.
        }
    }
}

Thank you very much :)This is my Application

Jananath Banuka
  • 2,951
  • 8
  • 57
  • 105
  • That array index `i` tells you which of the fields failed validation. Not sure why you stuff it into an array, though. Why not have a `boolean okay = validateTextField(jTextField1); okay &= validateTextField(jTextField2); okay &= validateTextField(jTextField3); .... ` (and give the textFields better variable names, too) – Thilo Oct 30 '16 at 09:36
  • @Thilo: No I think it gives the length of the text in the Jtextfield right? – Jananath Banuka Oct 30 '16 at 09:38
  • Here is code that does something similar: http://stackoverflow.com/questions/2749521/how-to-validate-a-jtextfield – Thilo Oct 30 '16 at 09:43
  • But how do I recognize which JTextfield is causing the problem? did you soo my GUI ? and can you show me the code? – Jananath Banuka Oct 30 '16 at 09:43
  • I think this is not what I'm looking for @Thilo – Jananath Banuka Oct 30 '16 at 09:44
  • In your array, if `check[0] == 0` then `jTextField1` is empty. If `check[1] == 0` then `jTextField2` is empty, and so on. – Thilo Oct 30 '16 at 09:45
  • @Thilo : what if both jTextField1 and jTextField2 are empty? How can I achieve something like that? – Jananath Banuka Oct 30 '16 at 09:49
  • Then both `check[0]` and `check[1]` will be `0`. – Thilo Oct 30 '16 at 09:50
  • @Thilo I think you are not understanding what I'm trying to tell you. I need to set background colors of the jTextFields whi the user have not entered the data. So can you please provide me a suitable code sample for that? – Jananath Banuka Oct 30 '16 at 09:52
  • isInputValid = true; for (int i = 0; i < check.length; i++) { if (check[i] == 0) { //needs to show which jTextfield/s is/are empty and make their backgrounds RED isInputValid = false; } } // now check if input are valid if(isInputValid) { //save to database----> I know what I have to do here. } else { return; } – johnII Oct 30 '16 at 09:53
  • but when is the Backgrounds are turning to RED? @johnII – Jananath Banuka Oct 30 '16 at 10:04
  • it will turn red right after the code executes – johnII Oct 30 '16 at 10:06
  • Sorry, any of you guys are not getting Really what I want. I know what you guys are telling me is right. i.e. when a Jtextfield is empty I cannot save data into the database. I know that and you are right. But I NEED TO INFORM THE USER THAT WHICH JTEXTFIELD IS/ARE EMPTY. not only if one or more JTextfields are empty i won't able to save the Details, but I need to inform the user specifically which JTextfield is causing the problem (that is empty). @johnII – Jananath Banuka Oct 30 '16 at 10:41
  • @Thilo can you explain what did you mean by this? "boolean okay = validateTextField(jTextField1); okay &= validateTextField(jTextField2); okay &= validateTextField(jTextField3); .... " – Jananath Banuka Oct 30 '16 at 10:47
  • Hello anybody please help me – Jananath Banuka Oct 30 '16 at 10:52

2 Answers2

0

For that you need to add change listener (a DocumentListener which reacts for change in the text) for your JTextField, and within actionPerformed(), you need to update the loginButton to enabled/disabled depending on the whether the JTextfield is empty or not.

You can add action listener to know does your textfield has changed, or not.

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

  public void changed() {
     if (yourJTextField.getText().equals("")){
       loginButton.setEnabled(false);
     }
     else {
       loginButton.setEnabled(true);
    }

  }
});

Then function checkempty would check for flags that could be set in changed(), Example changed() code:

boolean changedField = false;

public static void changed(){
    changedField = true;
}

And check does changedField is true.

Kamila Szewczyk
  • 1,874
  • 1
  • 16
  • 33
  • You want to check *all* text fields before enabling the button. – Thilo Oct 30 '16 at 09:52
  • Sorry I don't understand the second code sample. (Sample with boolean someflag) @Thilo – Jananath Banuka Oct 30 '16 at 09:55
  • Sorry I don't understand the second code sample. (Sample with boolean someflag) @Joel – Jananath Banuka Oct 30 '16 at 09:56
  • This documentlistener calls changed() everytime field gets changed. Changed sets the flag (for example boolean someflag), and you need to check `someflag==true` to prove that it has been changed. For more information check http://stackoverflow.com/questions/17132452/java-check-if-jtextfield-is-empty-or-not – Kamila Szewczyk Oct 30 '16 at 09:56
  • Sorry, any of you guys are not getting Really what I want. I know what you guys are telling me is right. i.e. when a Jtextfield is empty I cannot save data into the database. I know that and you are right. But I NEED TO INFORM THE USER THAT WHICH JTEXTFIELD IS/ARE EMPTY. not only if one or more JTextfields are empty i won't able to save the Details, but I need to inform the user specifically which JTextfield is causing the problem (that is empty). @Joel – Jananath Banuka Oct 30 '16 at 10:42
  • Just make jlabel and if flag is false, and in `public void changed() { if (yourJTextField.getText().equals("")){ loginButton.setEnabled(false); } else { loginButton.setEnabled(true); } }` replace loginButton.setEnabled false to some code to inform user about empty editor field. – Kamila Szewczyk Oct 30 '16 at 11:18
0

Do something like below:

public void checkEmpty() {
    JTextField [] textFields = {jTextField1,jTextField2,jTextField3,jTextField4,jTextField5,jTextField6};
    isInputValid = true;
    for (int i = 0; i < textFields.length; i++) {
        JTextField jTextField = textFields[i];
        String textValue = jTextField.getText().trim();
        if (textValue.length() == 0) {
            //turn background into red
            jTextField.setBackground(Color.RED);
            isInputValid = false;
        }
    }

    // now check if input are valid
    if(!isInputValid) return;

    //save to database----> I know what I have to do here.
}
johnII
  • 1,423
  • 1
  • 14
  • 20
  • @jognll Does this check all the empty Jtextfields and make their backgrounds RED? – Jananath Banuka Oct 30 '16 at 10:01
  • the code that make the field red is not included but you should put it inside the `if (check[i] == 0) {` statement – johnII Oct 30 '16 at 10:03
  • can you tell me that code please? Sorry, any of you guys are not getting Really what I want. I know what you guys are telling me is right. i.e. when a Jtextfield is empty I cannot save data into the database. I know that and you are right. But I NEED TO INFORM THE USER THAT WHICH JTEXTFIELD IS/ARE EMPTY. not only if one or more JTextfields are empty i won't able to save the Details, but I need to inform the user specifically which JTextfield is causing the problem (that is empty). – Jananath Banuka Oct 30 '16 at 10:44
  • Sorry, any of you guys are not getting Really what I want. I know what you guys are telling me is right. i.e. when a Jtextfield is empty I cannot save data into the database. I know that and you are right. But I NEED TO INFORM THE USER THAT WHICH JTEXTFIELD IS/ARE EMPTY. not only if one or more JTextfields are empty i won't able to save the Details, but I need to inform the user specifically which JTextfield is causing the problem (that is empty). – Jananath Banuka Oct 30 '16 at 10:50
  • see edited code, did some refactoring - hopefully you get the whole picture – johnII Oct 30 '16 at 11:08
  • @johnll Sorry It doesn't turn the empty jTextfields to RED – Jananath Banuka Oct 30 '16 at 11:48
  • Can you anyone please show me a complete code sample that works fine for my help? Please a Great Help, because Everything posted here is getting a problem again and again – Jananath Banuka Oct 30 '16 at 11:50
  • It worked to a little bit but can you tell me why you have insert a return keyword and make !isInputValid false in the below code fragment? and when I type again it won't turn to the white – Jananath Banuka Oct 30 '16 at 11:53
  • this `if(!isInputValid) return;' will check if some input are empty, if it is it will exit the method, it not it will proceed to save to the db. do make sure to set the background back to its original color once validation passed – johnII Oct 30 '16 at 12:05
  • Yes I have the problem with that. I'm new to Java and can you please tell me that code sample too??? Turning the Background color to white when user types something? – Jananath Banuka Oct 30 '16 at 12:12