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

2022-09-03 01:42:49

假设我有这样一个情况:我有一个(表Authors)和两个(Id和Name)。TableViewTableColumns

这是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);
    }
}

假设我有两个(txtId和txtName)。现在,我想将表单元格中的值绑定到.TextFieldsTextFields

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

我可以将 Name 绑定到 txtName,因为它是 一个 ,但我无法将 Id 绑定到 txtId,因为它是 .我的问题是:如何将txtId绑定到Id?TableColumnTextFieldauthorsNamePropertySimpleStringPropertyTableColumnTextFieldauthorsIdPropertySimpleIntegerPropertyTableColumn

附言:如果有必要,我可以提供工作示例。


答案 1

尝试:

txtId.textProperty().bindBidirectional(authorProps2.authorsIdProperty(), new NumberStringConverter());

答案 2

推荐