wwwww
wwwww

Reputation: 349

Binding two class properties with TableView column in JavaFX

I have following class:

public class Person {
    private final StringProperty firstName;
    private final StringProperty lastName;

    public String getFirstName() {
        return firstName.get();
    }

    public void setFirstName(String firstName) {
        this.firstName.set(firstName);
    }

    public StringProperty firstNameProperty() {
        return firstName;
    }

    public String getLastName() {
        return lastName.get();
    }

    public void setLastName(String lastName) {
        this.lastName.set(lastName);
    }

    public StringProperty lastNameProperty() {
        return lastName;
    }

    public StringProperty getName() {
        return new SimpleStringProperty(firstName.get() + " " + lastName.get()); 
    }
}

Then I have a TableView where I display both names in one column:

@FXML
private TableView<Person> personTable;
@FXML
private TableColumn<Person, String> name;

@FXML
private void initialize() {
    name.setCellValueFactory(cellData -> cellData.getValue().getName());

    personTable.getSelectionModel().selectedItemProperty().addListener(
            (observable, oldVal, newVal) -> showDetails(newVal));
}

And right now I can make changes to my Person object but they aren't displayed in TableView column. When I open edit window again I see new values but in my TableView I see old ones. What can I do about it?

Upvotes: 3

Views: 3893

Answers (1)

James_D
James_D

Reputation: 209225

Do

public class Person {

    private final ReadOnlyStringWrapper name = new ReadOnlyStringWrapper();

    private final StringProperty firstName = new SimpleStringProperty();
    private final StringProperty lastName = new SimpleStringProperty();

    public Person() {

        name.bind(Bindings.concat(firstName, " ", lastName));
        // existing code...
    }

    // existing code...

    public ReadOnlyStringProperty nameProperty() {
        return name.getReadOnlyProperty();
    }

    public final String getName() {
        return name.get();
    }
}

and then you can do

@FXML
private TableColumn<Person, String> name;

@FXML
private void initialize() {
    name.setCellValueFactory(cellData -> cellData.getValue().nameProperty());
}

Alternatively, you can omit the full name entirely from the Person class and just create the binding where you need it in the cell value factory:

name.setCellValueFactory(cellData -> Bindings.concat(
    cellData.getValue().firstNameProperty(),
    " ",
    cellData.getValue().lastNameProperty()));

Upvotes: 7

Related Questions