Albert
Albert

Reputation: 11

Setting values from a TextField

I´m developing some sort of .csv reader/editor. To do so, I created a class (PersonData) with a fileReader method (importPersonData) that parses the data from a CSV file and creates the variables and its corresponding getters and setters. The program shows the values in the UI in a TextField so that the user can edit them. I want the program to change the value in the PersonData class upon clicking "Save" button, but I can´t figure out how. This is what the main class looks like (I omitted the UI parts for simplicity):

public class GUI extends Application implements EventHandler<ActionEvent> {
    List<PersonData> people = new ArrayList<PersonData>();

    public void start(Stage primaryStage) throws Exception {
        File getPersonData = new File ("personData.csv");
        String personDataPath  = getPersonData.getAbsolutePath();
        PersonData.importPersonData(personDataPath,people);

        int dataSize=people.size();
        TextField personNametf[] = new TextField[dataSize];

        int n=0;
        for(PersonData p: people){
            Label personNameLabel = new Label("Name: ");
            //call personName getter and set is as default text field
            personNametf[n] = new TextField(p.getPersonName());
}

    saveButton = new Button("Save");
    saveButton.setOnAction(this);

    @Override
    public void handle(ActionEvent actionEvent) {
        if (actionEvent.getSource()==saveButton){
            int n=0;
            for (PersonData p: people) {
                p.setPersonName(personNametf[n].getText());
            n=n+1;
            }
        }
    }
}             

I understand that I can´t use personNametf in the ActionListener method because it belongs to a different method. I can´t declare it at a class level either because the array is empty at this point. I thought about using binded properties, but don´t really know how to work it out.

Upvotes: 1

Views: 51

Answers (1)

James_D
James_D

Reputation: 209330

Just make the text field array an instance variable:

public class GUI extends Application implements EventHandler<ActionEvent> {
    List<PersonData> people = new ArrayList<PersonData>();

    private TextField[] personNametf ;

    public void start(Stage primaryStage) throws Exception {
        File getPersonData = new File ("personData.csv");
        String personDataPath  = getPersonData.getAbsolutePath();
        PersonData.importPersonData(personDataPath,people);

        int dataSize=people.size();
        personNametf = new TextField[dataSize];

        int n=0;
        for(PersonData p: people){
            Label personNameLabel = new Label("Name: ");
            //call personName getter and set is as default text field
            personNametf[n++] = new TextField(p.getPersonName());
        }

        saveButton = new Button("Save");
        saveButton.setOnAction(this);
    }

    @Override
    public void handle(ActionEvent actionEvent) {
        if (actionEvent.getSource()==saveButton){
            int n=0;
            for (PersonData p: people) {
                p.setPersonName(personNametf[n].getText());
                n=n+1;
            }
        }
    }
} 

The event handler can't be invoked without the save button being displayed, so the start() method is guaranteed to be invoked before the event handler. Thus you're guaranteed that the array is initialized before you access it in the event handler.

Upvotes: 2

Related Questions