How to point out which jTextfield is empty [duplicate]

大兔子大兔子 提交于 2019-12-24 00:32:55

问题


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


回答1:


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.
}



回答2:


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.



来源:https://stackoverflow.com/questions/40327694/how-to-point-out-which-jtextfield-is-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!