我有一个ObservableList<Member>
要显示在TableView<Member>
.
该类Member
由 a Person
(和其他不重要的值组成,因为它们不会出现在 tableview 中)。每个Person
人都有一个StringProperty
“名字”和一个ObjectProperty<Dog>
“狗”。每只狗都有一个StringProperty
“名字”。
tableview 应该在第一列显示人的名字,在第二列显示狗的名字。像这样:示例表视图
我设法通过绑定TableColumn<Member, Person>
到member.personProperty()
自定义单元工厂并使用自定义单元工厂来显示人名或狗名来实现这一点。
现在,我还必须ComboBox
es 通过它用户能够更新TableView
. valueProperty()
因此,我在所选项目和组合框之间创建了绑定。
如果表格视图通过组合框更改人名的单元格,则会更新该单元格,而狗名的单元格不会自动显示新值。我知道这是因为Member
没有收到有关狗属性更改的通知。但是我还没有找到一个解决方案,如何让一个对象知道它的子对象中的变化。
所以我想,总而言之,我的问题是(1)如何在平面表视图中显示嵌套对象,以及(2)如果任何(可能)嵌套属性发生变化,如何自动更新表视图。
示例代码:
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"))));
}
}