How to check which JTextFields are empty and inform the user about them?

风流意气都作罢 提交于 2019-12-04 07:05:48

问题


I have a code sample with JTextFields. When a user does not enter the details in some JTextField, and then clicks the "Register" button, I want the user to be informed about his mistake/s. Like this:

You haven't entered Student Middle Name

or

You have not entered Student Address

or

You haven't entered Student middle name and Address

and not as

You have not entered all the details

I want the information to be in a JLabel.

I also want that the JTextField/s that is/are empty to have its/their background/s set as red.

I have tried many codes but none of them worked.

Here's my code. I have used the array to check which JTextField/s are empty, but I don't know how to inform the user about them.

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) {

        } else {

        }
    }
}

回答1:


Here is a simple example. instead of iterating over the length of the inputs, iterate over the fields themselves. If one is empty paint it red and append its corresponding message to the list of messages. Then display that list on a label.

public class Main {

    public static void main(String[] args) {

        JTextField fName = new JTextField();
        fName.setName("First Name");
        JTextField lName = new JTextField();
        lName.setName("last Name");
        JTextField address = new JTextField();
        address.setName("Address");

        JTextField[] fields = new JTextField[] {fName, lName, address};

        StringBuilder sb = new StringBuilder("<html>");
        for (JTextField field : fields) {
            if (field.getText().isEmpty()){
                field.setBackground(Color.RED);
                sb.append("<p>").append("You haven't entered " + field.getName());
            }
        }

        JLabel label = new JLabel(sb.toString());
    }
}



回答2:


Create an Array of error messages:

String[] errors = 
{
    "First Name is missing",
    "Middle Name is missing",
    ...
};

Then in your looping code you can just reference the Array for the appropriate message. You might want to use a JTextArea since you could have multiple message:

if (check[i] == 0) 
{
    textArea.append( errors[i] );
}

You could even simplify your code by create an Array of text fields then your looping code would be something like:

for (int i = 0; i < textFields.length; i++) 
{
    JTextField textField = textFields[i];
    String text = textField.getText();
    int length = text.length();

    if (length == 0) 
    {
        textArea.append( ... );
    }
}

Look to create loops/reusable code. It makes maintenance easier if you ever need to add another text field to test.



来源:https://stackoverflow.com/questions/40328377/how-to-check-which-jtextfields-are-empty-and-inform-the-user-about-them

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