Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working with javafx and fxml to make a simple project.

I have a method in my project which is supposed to clear all textfields and comboboxes.

Rather than setting each textfields and comboboxes values to null one by one. I put all the nodes inside an arraylist so I can iterate and set their values to null.

I just want to know if there is a cleaner approach to this or if I'm doing this right. I'm new to javafx and fxml and a lot of things are unknown to me.

What I have tried:

This is my code for the clear button

Java
@FXML
private ArrayList <TextField> textfieldList;
private void clear()
{
    for(Node node : textfieldList)
    {
        if(node instanceof TextField)
        {
            ((TextField) node).setText(null);
        }
        else if(node instanceof ComboBox<?>)
        {
            ((ComboBox<?>) node).getSelectionModel().select(-1);
        }
    }
}
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900