Подтвердить что ты не робот

JavaFX 8 - Как связать текстовое свойство TextField с атрибутом TableView integer

Скажем, у меня есть такая ситуация: у меня есть 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);
    }
}

И пусть говорят, что у меня два 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 к txtName TextField, потому что authorsNameProperty является SimpleStringProperty, но я не могу привязать Id TableColumn к txtId TextField, потому что authorsIdProperty является SimpleIntegerProperty. Мой вопрос: как я могу привязать txtId к Id TableColumn?

P.S. Я могу предоставить рабочий пример, если это необходимо.

4b9b3361

Ответ 1

Try:

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