简体   繁体   中英

How to check if all my textfields are empty JavaFX

Hey Guys I'm quite new in the world of programming and at the moment I'm working on a JavaFX application. In my application I want to check if all my textfields are empty. If all the textfields are filled than a create button should be enabled. My Problem is that it also enables the button when there are still some textfields empty.

I tried it with a for loop but obviously i did something wrong here. It only checks the last textfield in my Arraylist.

any help is appreciated!

List<TextField> textFields = Arrays.asList(nameEnemy, experienceEnemy, goldEnemy, attackEnemy)

            for (TextField field : textFields) {
                if ((!field.getText().isEmpty()) && (imageEnemy.getImage() != null)) {
                    createDataButton.setDisable(false);
                } else {
                    createDataButton.setDisable(true);
                }

You are setting the disable property of createDataButton every time. So it only holds the result for the last field checked. You need to exit the loop if any field causes it to be disabled. That also means you only have to enable it once (always) at the top of the method.

createDataButton.setDisable(false);
if (imageEnemy.getImage() == null) {
    createDataButton.setDisable(true);
} else {
    List<TextField> textFields = Arrays.asList(nameEnemy, experienceEnemy, goldEnemy, attackEnemy)
    for (TextField field : textFields) {
        if (field.getText().isEmpty()) {
            createDataButton.setDisable(true);
            break;
        }
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM