问题描述
假设我有这样的情况:我有一个 TableView
(tableAuthors),其中包含两个 TableColumns
(Id和名称)。
Let's say I have a situation like this: I have a TableView
(tableAuthors) with two TableColumns
(Id and Name).
这是AuthorProps POJO,由 TableView使用
:
This is the AuthorProps POJO which is used by 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
。
And let's say I have two TextFields
(txtId and txtName). Now, I would like to bind values from table cells to 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
?
I can bind Name TableColumn
to txtName TextField
because authorsNameProperty
is a SimpleStringProperty
, but I can't bind Id TableColumn
to txtId TextField
because authorsIdProperty
is a SimpleIntegerProperty
. My question is: How can I bind txtId to Id TableColumn
?
P.S。如果有必要,我可以提供工作示例。
P.S. I can provide working example if it's necessary.
推荐答案
尝试:
txtId.textProperty().bindBidirectional(authorProps2.authorsIdProperty(), new NumberStringConverter());
这篇关于JavaFX 8 - 如何将TextField文本属性绑定到TableView整数属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!