Save only if multiple JTextFields are not empty

与世无争的帅哥 提交于 2021-02-11 07:21:07

问题


I have multiple JTextFields and JComboBox in my JFrame. So whenever I click the _Add_ button it will check if the four (4) text fields in Current Medication Panel is Empty. If it is not then Execute, but it also depends if the Personal info Panel text fields are filled.

But I have a problem when I use the if and else statement, if I use the if and else:

    if(condition if the first textfield is empty) {
        // execute something like the textfield turn to red
    } else if(condition if the second textfield is empty) {
        // execute something like the textfield turn to red
    } else if(condition if the third textfield is empty) {
        // execute something like the textfield turn to red
    } else{
        // execute save information of the patient
    }

In this situation if the 1st text field is empty then it will turn to red but if both 1st and 2nd text field is empty only the 1st text field turn to red.

I also tried the if, and if and if but were should put the else whenever there is no empty or invalid input where it will execute and save the patient info like this:

   if(condition if the first textfield is empty) {
     // execute something like the textfield turn to red
   }
   if(condition if the second textfield is empty) {
     // execute something like the textfield turn to red
   }
   if(condition if the third textfield is empty) {
     // execute something like the textfield turn to red
   }
   if(condition if the fourth textfield is empty) {
     // execute something like the textfield turn to red
   } else

If I use this only the last if statement only works for the else statement. So if the last statement is true then execute, but not then else statement execute which is patient save info.

Is there any thing I can do about this? or is there any tutorial for me to learn more about Java and about if and else?


回答1:


In the Add button action listener's actionPerformed method, you can try this:

public void actionPerformed(ActionEvent e) {

    if (! textFieldsValid()) {
        // one or more of the text fields are empty
        // may be display a message in a JOptionPane
        System.out.println("The text fields are not filled with data, etc...");
        return;
    }

    // else, text fields have valid data, so do whatever processing it further...
}

/*
 * This method checks if the text fields are empty and sets their borders as red. Returns
 * a boolean false in case any of the text fields are empty, else true.
 */
private boolean textFieldsValid() {

    boolean validTextFields = true;

    if (textField1.getText().isEmpty()) {
        validTextFields = false;
        textField1.setBorder(new LineBorder(Color.RED));
    }

    if (textField2.getText().isEmpty()) {
        validTextFields = false;
        // textField2.setBorder(...)
    }

    // ... same with textField3 and textField4

    return validTextFields;
}



回答2:


but were should put the else

It is not mandatory to follow if with else. The purpose of specifying else is to allow your code execution flow to go through all other case when if was not satisfied (true).

if i use this only the last if statement only works for the else statement

Because, if might have satisfied, so it executes else case. I would suggest to include return in each if case. So that, if any of the if case was satisfied. Then, it won't execute further code.




回答3:


This should not be news to you: you are doing it wrong.

There are several ways to implement your desired solution, here is one of them:

boolean performSave = true;

if (field1 is empty)
{
    do some stuff.
    performSave = false;
}

if (field2 is empty)
{
    do some stuff.
    performSave = false;
}

... repeat for any number of fields.

if (performSave) // no fields are empty.
{
    save stuff.
}


来源:https://stackoverflow.com/questions/53915850/save-only-if-multiple-jtextfields-are-not-empty

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