如果嵌套对象绑定发生变化,如何更新 JavaFX TableView?
How to update JavaFX TableView if nested object binding changes?
我有 ObservableList<Member>
个成员,我想在 TableView<Member>
中显示。
Member
class 由 Person
组成(以及其他不重要的值,因为它们不会出现在 table 视图中)。每个 Person
都有一个 StringProperty
"name" 和一个 ObjectProperty<Dog>
"dog"。另外每只狗都有一个 StringProperty
"name".
table视图应该在第一列显示人名,在第二列显示狗名。像这样:Example table view
我设法通过将 TableColumn<Member, Person>
绑定到 member.personProperty()
并使用自定义单元格工厂来显示人名或狗名来实现这一点。
现在,我还必须 ComboBox
es,用户可以通过它更新 TableView
的选定行中的名称。因此,我在所选项目和组合框的 valueProperty()
之间创建了绑定。
虽然 table 视图会更新人名单元格(如果通过组合框进行更改),但狗名单元格不会自动显示新值。我知道这是因为 Member
没有收到有关狗 属性 更改的通知。但是我还没有找到如何让一个对象知道它的子对象中的变化的解决方案。
所以我想,总而言之,我的问题是 (1) 如何在平面 table 视图中显示嵌套对象以及 (2) 如何自动更新 table 视图(潜在的)嵌套属性发生变化。
示例代码:
package sample;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.util.StringConverter;
public class ControllerString {
private class Dog
{
private final StringProperty name;
public Dog(String name) {
this.name = new SimpleStringProperty(name);
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
}
private class Person
{
private final StringProperty name;
private final ObjectProperty<Dog> dog;
public Person(String name, Dog dog) {
this.name = new SimpleStringProperty(name);
this.dog = new SimpleObjectProperty<>(dog);
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public Dog getDog() {
return dog.get();
}
public ObjectProperty<Dog> dogProperty() {
return dog;
}
public void setDog(Dog dog) {
this.dog.set(dog);
}
}
private class Member
{
private final ObjectProperty<Person> person;
public Member(Person person) {
this.person = new SimpleObjectProperty<>(person);
}
public Person getPerson() {
return person.get();
}
public ObjectProperty<Person> personProperty() {
return person;
}
public void setPerson(Person person) {
this.person.set(person);
}
}
@FXML
private TableView<Member> membersTableView;
@FXML
private TableColumn<Member, Person> nameTableColumn;
@FXML
private TableColumn<Member, Person> dogTableColumn;
@FXML
private ComboBox<Person> nameComboBox;
@FXML
private ComboBox<Dog> dogComboBox;
private final ObservableList<Member> members = FXCollections.observableArrayList();
@FXML
private void initialize()
{
nameTableColumn.setCellValueFactory(cellData -> cellData.getValue().personProperty());
nameTableColumn.setCellFactory(column -> new TableCell<Member, Person>() {
@Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
setContentDisplay(ContentDisplay.TEXT_ONLY);
if(person == null || empty)
{
setText(null);
}
else
{
setText(person.getName());
}
}
});
dogTableColumn.setCellValueFactory(cellData -> cellData.getValue().personProperty());
dogTableColumn.setCellFactory(column -> new TableCell<Member, Person>()
{
@Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
setContentDisplay(ContentDisplay.TEXT_ONLY);
if(person == null || empty)
{
setText(null);
}
else
{
setText(person.getDog().getName());
}
}
});
nameComboBox.setConverter(new StringConverter<Person>() {
@Override
public String toString(Person person) {
if(person == null)
{
return null;
}
return person.getName();
}
@Override
public Person fromString(String name) {
return new Person(name, new Dog("Puppy"));
}
});
dogComboBox.setConverter(new StringConverter<Dog>() {
@Override
public String toString(Dog dog) {
if(dog == null)
{
return null;
}
return dog.getName();
}
@Override
public Dog fromString(String name) {
return new Dog(name);
}
});
membersTableView.getSelectionModel().getSelectedItems().addListener((ListChangeListener<Member>) change -> {
while(change.next()) {
if(change.wasRemoved()) {
Member oldValue = change.getRemoved().get(0);
nameComboBox.valueProperty().unbindBidirectional(oldValue.personProperty());
}
if(change.wasAdded()) {
Member newValue = change.getAddedSubList().get(0);
nameComboBox.valueProperty().bindBidirectional(newValue.personProperty());
}
}
});
nameComboBox.valueProperty().addListener(((observable, oldValue, newValue) -> {
if(oldValue != null) {
dogComboBox.valueProperty().unbindBidirectional(oldValue.dogProperty());
}
if(newValue != null)
{
dogComboBox.valueProperty().bindBidirectional(newValue.dogProperty());
}
}));
membersTableView.setItems(members);
members.add(new Member(new Person("Bob", new Dog("Caesar"))));
}
}
如果您将 Person
和 Dog
的可见性修改为 public
,Bindings.select
可以与 cellValueFactory
一起使用,这样您就可以得到摆脱自定义 TableCell
实现:
public class Dog {
...
}
public class Person {
...
}
...
@FXML
private TableColumn<Member, String> nameTableColumn;
@FXML
private TableColumn<Member, String> dogTableColumn;
...
@FXML
private void initialize() {
nameTableColumn.setCellValueFactory(cellData -> Bindings.select(cellData.getValue().personProperty(), "name"));
dogTableColumn.setCellValueFactory(cellData -> Bindings.select(cellData.getValue().personProperty(), "dog", "name"));
...
如果其中一个中间属性可以包含 null
,那么您可以预期这会导致很多警告...
我有 ObservableList<Member>
个成员,我想在 TableView<Member>
中显示。
Member
class 由 Person
组成(以及其他不重要的值,因为它们不会出现在 table 视图中)。每个 Person
都有一个 StringProperty
"name" 和一个 ObjectProperty<Dog>
"dog"。另外每只狗都有一个 StringProperty
"name".
table视图应该在第一列显示人名,在第二列显示狗名。像这样:Example table view
我设法通过将 TableColumn<Member, Person>
绑定到 member.personProperty()
并使用自定义单元格工厂来显示人名或狗名来实现这一点。
现在,我还必须 ComboBox
es,用户可以通过它更新 TableView
的选定行中的名称。因此,我在所选项目和组合框的 valueProperty()
之间创建了绑定。
虽然 table 视图会更新人名单元格(如果通过组合框进行更改),但狗名单元格不会自动显示新值。我知道这是因为 Member
没有收到有关狗 属性 更改的通知。但是我还没有找到如何让一个对象知道它的子对象中的变化的解决方案。
所以我想,总而言之,我的问题是 (1) 如何在平面 table 视图中显示嵌套对象以及 (2) 如何自动更新 table 视图(潜在的)嵌套属性发生变化。
示例代码:
package sample;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
import javafx.collections.FXCollections;
import javafx.collections.ListChangeListener;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.scene.control.*;
import javafx.util.StringConverter;
public class ControllerString {
private class Dog
{
private final StringProperty name;
public Dog(String name) {
this.name = new SimpleStringProperty(name);
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
}
private class Person
{
private final StringProperty name;
private final ObjectProperty<Dog> dog;
public Person(String name, Dog dog) {
this.name = new SimpleStringProperty(name);
this.dog = new SimpleObjectProperty<>(dog);
}
public String getName() {
return name.get();
}
public StringProperty nameProperty() {
return name;
}
public void setName(String name) {
this.name.set(name);
}
public Dog getDog() {
return dog.get();
}
public ObjectProperty<Dog> dogProperty() {
return dog;
}
public void setDog(Dog dog) {
this.dog.set(dog);
}
}
private class Member
{
private final ObjectProperty<Person> person;
public Member(Person person) {
this.person = new SimpleObjectProperty<>(person);
}
public Person getPerson() {
return person.get();
}
public ObjectProperty<Person> personProperty() {
return person;
}
public void setPerson(Person person) {
this.person.set(person);
}
}
@FXML
private TableView<Member> membersTableView;
@FXML
private TableColumn<Member, Person> nameTableColumn;
@FXML
private TableColumn<Member, Person> dogTableColumn;
@FXML
private ComboBox<Person> nameComboBox;
@FXML
private ComboBox<Dog> dogComboBox;
private final ObservableList<Member> members = FXCollections.observableArrayList();
@FXML
private void initialize()
{
nameTableColumn.setCellValueFactory(cellData -> cellData.getValue().personProperty());
nameTableColumn.setCellFactory(column -> new TableCell<Member, Person>() {
@Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
setContentDisplay(ContentDisplay.TEXT_ONLY);
if(person == null || empty)
{
setText(null);
}
else
{
setText(person.getName());
}
}
});
dogTableColumn.setCellValueFactory(cellData -> cellData.getValue().personProperty());
dogTableColumn.setCellFactory(column -> new TableCell<Member, Person>()
{
@Override
protected void updateItem(Person person, boolean empty) {
super.updateItem(person, empty);
setContentDisplay(ContentDisplay.TEXT_ONLY);
if(person == null || empty)
{
setText(null);
}
else
{
setText(person.getDog().getName());
}
}
});
nameComboBox.setConverter(new StringConverter<Person>() {
@Override
public String toString(Person person) {
if(person == null)
{
return null;
}
return person.getName();
}
@Override
public Person fromString(String name) {
return new Person(name, new Dog("Puppy"));
}
});
dogComboBox.setConverter(new StringConverter<Dog>() {
@Override
public String toString(Dog dog) {
if(dog == null)
{
return null;
}
return dog.getName();
}
@Override
public Dog fromString(String name) {
return new Dog(name);
}
});
membersTableView.getSelectionModel().getSelectedItems().addListener((ListChangeListener<Member>) change -> {
while(change.next()) {
if(change.wasRemoved()) {
Member oldValue = change.getRemoved().get(0);
nameComboBox.valueProperty().unbindBidirectional(oldValue.personProperty());
}
if(change.wasAdded()) {
Member newValue = change.getAddedSubList().get(0);
nameComboBox.valueProperty().bindBidirectional(newValue.personProperty());
}
}
});
nameComboBox.valueProperty().addListener(((observable, oldValue, newValue) -> {
if(oldValue != null) {
dogComboBox.valueProperty().unbindBidirectional(oldValue.dogProperty());
}
if(newValue != null)
{
dogComboBox.valueProperty().bindBidirectional(newValue.dogProperty());
}
}));
membersTableView.setItems(members);
members.add(new Member(new Person("Bob", new Dog("Caesar"))));
}
}
如果您将 Person
和 Dog
的可见性修改为 public
,Bindings.select
可以与 cellValueFactory
一起使用,这样您就可以得到摆脱自定义 TableCell
实现:
public class Dog {
...
}
public class Person {
...
}
...
@FXML
private TableColumn<Member, String> nameTableColumn;
@FXML
private TableColumn<Member, String> dogTableColumn;
...
@FXML
private void initialize() {
nameTableColumn.setCellValueFactory(cellData -> Bindings.select(cellData.getValue().personProperty(), "name"));
dogTableColumn.setCellValueFactory(cellData -> Bindings.select(cellData.getValue().personProperty(), "dog", "name"));
...
如果其中一个中间属性可以包含 null
,那么您可以预期这会导致很多警告...