JavaFX 8 - 如何将TextField文本属性绑定到TableView整数属性

Bra*_*zic 14 java javafx javafx-8

假设我有这样的情况:我有一个TableView(tableAuthors)有两个TableColumns(Id和Name).

这是AuthorProps POJO,用于TableView:

import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleStringProperty;


public class AuthorProps {
    private final SimpleIntegerProperty authorsId;
    private final SimpleStringProperty authorsName;


    public AuthorProps(int authorsId, String authorsName) {
        this.authorsId = new SimpleIntegerProperty(authorsId);
        this.authorsName = new SimpleStringProperty( authorsName);
    }

    public int getAuthorsId() {
        return authorsId.get();
    }

    public SimpleIntegerProperty authorsIdProperty() {
        return authorsId;
    }

    public void setAuthorsId(int authorsId) {
        this.authorsId.set(authorsId);
    }

    public String getAuthorsName() {
        return authorsName.get();
    }

    public SimpleStringProperty authorsNameProperty() {
        return authorsName;
    }

    public void setAuthorsName(String authorsName) {
        this.authorsName.set(authorsName);
    }
}
Run Code Online (Sandbox Code Playgroud)

让我们说我有两个TextFields(txtId和txtName).现在,我想将表格单元格中的值绑定到TextFields.

 tableAuthors.getSelectionModel()
                .selectedItemProperty()
                .addListener((observableValue, authorProps, authorProps2) -> {
                    //This works:
                    txtName.textProperty().bindBidirectional(authorProps2.authorsNameProperty());
                    //This doesn't work:
                    txtId.textProperty().bindBidirectional(authorProps2.authorsIdProperty());
                });
Run Code Online (Sandbox Code Playgroud)

我可以将Name绑定TableColumn到txtName,TextField因为它authorsNameProperty是a SimpleStringProperty,但是我无法将Id绑定TableColumn到txtId,TextField因为它authorsIdProperty是a SimpleIntegerProperty.我的问题是:如何将txtId绑定到Id TableColumn

PS如果有必要,我可以提供工作示例.

Jam*_*s_D 17

尝试:

txtId.textProperty().bindBidirectional(authorProps2.authorsIdProperty(), new NumberStringConverter());
Run Code Online (Sandbox Code Playgroud)