1

I wrote this method, because I want to check all of places if textfields, datepicker and radiobuttons are empty when I will press a button, but it doesn't work.

What should I correct in this code, or should I do a method in different way?

public void btn1(ActionEvent event) throws Exception {
    if(name.getText().trim().isEmpty() && surname.getText().trim().isEmpty() && pesel.getText().trim().isEmpty() && adres.getText().trim().isEmpty() && email.getText().trim().isEmpty() )
        if(dateOfBirth.getValue() == null)
            if(sexM.isSelected() || sexF.isSelected() || sexG.isSelected()){
                lbl.setText("All of places have to be filled");
            } else {
                 Stage primaryStage = new Stage();
                 Parent root = FXMLLoader.load(getClass().getResource("\\Step2.fxml"));
                 Scene scene = new Scene(root);
                 scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
                 primaryStage.setScene(scene);
                 primaryStage.show();
            }
        }
    }
}
Developer66
  • 732
  • 6
  • 17
XtrEmE
  • 13
  • 6

3 Answers3

0

Try this:

public void btn1(ActionEvent event) throws Exception {
    if (name.getText().trim().length() == 0 && surname.getText().trim().length() == 0
            && pesel.getText().trim().length() == 0 && adres.getText().trim().length() == 0
            && email.getText().trim().length() == 0 && dateOfBirth.getValue() == null && sexM.isSelected()
            || sexF.isSelected() || sexG.isSelected()) {
        lbl.setText("All of places have to be filled");
    } else {
        Stage primaryStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("\\Step2.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

Replace all isEmpty() with .length()

Developer66
  • 732
  • 6
  • 17
0

You'll notice there is a lot of repeated code when checking the TextFields. If would be better to get rid of this.

Furthermore by using 3 ifs you probably made some mistake: You allow for cases where neither a new stage is shown nor the label is set to an error message.

Also the part with the sexes looks like you use CheckBoxes/RadioButtons that are mutual exclusive. In this case you should use a ToggleGroup which allows you to check for a selection using code similar to the one used with the DatePicker:

/**
 * Helper method for checking, none of the TextFields passed are empty.
 */
private static boolean allFilled(TextField... textFields) {
    for (TextField textField : textFields) {
        if (textField.getText().trim().isEmpty()) {
            return false;
        }
    }
    return true;
}
ToggleGroup sexToggleGroup = new ToggleGroup();

sexM.setToggleGroup(sexToggleGroup);
sexF.setToggleGroup(sexToggleGroup);
sexG.setToggleGroup(sexToggleGroup);
if (allFilled(name, surname, pesel, adres, email)
      && dateOfBirth.getValue() != null
      && sexToggleGroup.getSelectedToggle() != null) {
    ... show new stage ...
} else {
    ... display error message ...
}
fabian
  • 80,457
  • 12
  • 86
  • 114
0

It's my solution. Maybe that will help someone.

@FXML
public void btn1(ActionEvent event) throws IOException {

    TextField[] fieldList = new TextField[]{name, surname, pesel, adres, email};

    ToggleGroup sexToggleGroup = new ToggleGroup();     
    sexM.setToggleGroup(sexToggleGroup);
    sexF.setToggleGroup(sexToggleGroup);
    sexG.setToggleGroup(sexToggleGroup);

    boolean right = false;

    for(TextField listF : fieldList)
    if(listF.getText().equals("") || dateOfBirth.getEditor().getText().equals("") || sexToggleGroup.getSelectedToggle() == null){
        lbl.setText("All of places have to be filled");
        right = true;
        break;
    } else {
        right = false;
    }
    if(right == false){
        Stage primaryStage = new Stage();
        Parent root = FXMLLoader.load(getClass().getResource("\\Step2.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.show();
    }
 }
XtrEmE
  • 13
  • 6