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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22622636/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:54:55  来源:igfitidea点击:

JavaFX 8 - How to bind TextField text property to TableView integer property

javajavafxjavafx-8

提问by Branislav Lazic

Let's say I have a situation like this: I have a TableView(tableAuthors) with two TableColumns(Id and Name).

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

This is the AuthorProps POJO which is used by TableView:

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

And let's say I have two TextFields(txtId and txtName). Now, I would like to bind values from table cells to TextFields.

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

I can bind Name TableColumnto txtName TextFieldbecause authorsNamePropertyis a SimpleStringProperty, but I can't bind Id TableColumnto txtId TextFieldbecause authorsIdPropertyis a SimpleIntegerProperty. My question is: How can I bind txtId to Id TableColumn?

我可以将 Name 绑定TableColumn到 txtNameTextField因为authorsNameProperty是 a SimpleStringProperty,但我不能将 Id 绑定TableColumn到 txtIdTextField因为authorsIdProperty是 a SimpleIntegerProperty。我的问题是:如何将 txtId 绑定到 Id TableColumn

P.S. I can provide working example if it's necessary.

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

采纳答案by James_D

Try:

尝试:

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