-2

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 {

        }
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
Jananath Banuka
  • 2,951
  • 8
  • 57
  • 105

2 Answers2

2

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());
    }
}
user1803551
  • 12,965
  • 5
  • 47
  • 74
  • Here field.getName() will prints null. to overcome this problem can I use different Names corresponding to each jTextfield? – Jananath Banuka Oct 30 '16 at 18:26
  • @CodeBae `getName` will return what was set in `setName`. Look at my example. – user1803551 Oct 30 '16 at 18:35
  • 1
    Nice job of spoon feeding. Notice how the OP hasn't really learned anything because he didn't even take the time to understand your code to see what the setName() method was doing. Even when the OP asked me for the "complete" code I only gave the OP hints to force him to think about logically solving the problem on his own. – camickr Oct 30 '16 at 18:56
  • @camickr : I'm sorry :( – Jananath Banuka Oct 30 '16 at 19:12
1

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.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • That was really helpful and can you please give me the complete code? @camickr – Jananath Banuka Oct 30 '16 at 16:45
  • I was looking into the Error messages array as you said. But it will prints all the messages when even some jTextfields are not empty. How can I resolve that? Thanks – Jananath Banuka Oct 30 '16 at 17:04
  • 1
    So debug your code and figure out why your logic isn't working correctly. Did you display the value of the variable your are testing? That is the point of programming. You can't expect to ask a question every time you come across a little problem. – camickr Oct 30 '16 at 17:11
  • Thanks for your help. But can I have a little more question? – Jananath Banuka Oct 30 '16 at 17:22
  • when I click Register button with some empty Jtextfields, the Text area will shows the error Messages. then When I click the button again with different jTextFields empty, it appends the error messages with the previous error messages (some of which jTextfields are now NOT EMPTY). How can I resolve that? can you please help me? – Jananath Banuka Oct 30 '16 at 17:24
  • Well you need to clear the text area BEFORE you start your loop. So you can set the text in the text area to the empty string. – camickr Oct 30 '16 at 18:51