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

2024-04-22

假设我有这样的情况:我有一个TableView(表作者)有两个TableColumns(身份证号和姓名)。

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

假设我有两个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());
                });

我可以绑定姓名TableColumn到txt名称TextField因为authorsNameProperty is a SimpleStringProperty,但我无法绑定IdTableColumn到 txtIdTextField因为authorsIdProperty is a SimpleIntegerProperty。我的问题是:如何将 txtId 绑定到 IdTableColumn?

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


Try:

txtId.textProperty().bindBidirectional(authorProps2.authorsIdProperty(), new NumberStringConverter());
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐