我不久前创建了一个 TableView,并为每个 TableColumns 注册了属性。对内部数据的编辑会很好地反射(reflect)在 TableView 中。
但是,对于 ListView,情况就不同了。除非我关闭框架并再次打开它,否则不会立即显示更改。
我的 ListView 由 ActionSteps 组成。请注意,我使用了 Javafx bean 属性。
package application.objects;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.function.IntPredicate;
import javafx.beans.property.IntegerProperty;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.beans.property.SimpleStringProperty;
import javafx.beans.property.StringProperty;
public class ActionStep {
private StringProperty actionStepID;
private ObjectProperty<LocalDate> dateSet, dateFinished;
private StringProperty stepName;
private IntegerProperty completion;
private ArrayList<StepComment> comments;
public ActionStep(String name) {
actionStepID = new SimpleStringProperty();
stepName = new SimpleStringProperty();
dateSet = new SimpleObjectProperty<LocalDate>();
dateFinished = new SimpleObjectProperty<LocalDate>();
completion = new SimpleIntegerProperty();
stepName.setValue(name);
}
public void setName(String name) {
stepName.setValue(name);
}
public String getName() {
return stepName.getValue();
}
public StringProperty stepNameProperty() {
return actionStepID;
}
public void setID(String id) {
actionStepID.setValue(id);
}
public String getID() {
return actionStepID.get();
}
public StringProperty actionStepIDProperty() {
return actionStepID;
}
public void setCompletion(int percent) {
if (percent < 0 || percent > 100)
return;
completion.set(percent);
}
public int getCompletion() {
return completion.get();
}
public IntegerProperty completionProperty() {
return completion;
}
public void setDateSet(LocalDate date) {
dateSet.set(date);
}
public LocalDate getDateSet() {
return dateSet.get();
}
public ObjectProperty<LocalDate> dateSetProperty() {
return dateSet;
}
public void setDateFinished(LocalDate date) {
dateFinished.set(date);
}
public LocalDate getDateFinished() {
return dateFinished.get();
}
public ObjectProperty<LocalDate> dateFinishedProperty() {
return dateFinished;
}
public String toString() {
return stepNameProperty().get();
}
}
我的 ListView 也使用 ObservableList。
@FXML
private ListView<ActionStep> actionStepsListView;
private ObservableList<ActionStep> listOfSteps;
listOfSteps = FXCollections.observableArrayList();
actionStepsListView.setItems(listOfSteps);
if (plan != null) {
ArrayList<ActionStep> arrayOfSteps = plan.getStepsArrayList();
for (int i = 0; i < arrayOfSteps.size(); i++)
listOfSteps.add(arrayOfSteps.get(i));
} else
plan = new ActionPlan();
为什么对 ObservableList 所做的更改不会反射(reflect)在 ListView 中?我注意到 ListView 调用每个对象的 toString() 以在 ListView 中显示它们的值,而不是将其绑定(bind)到它们的属性。
我做错了什么?我应该覆盖细胞工厂还是什么?
最佳答案
请注意,与 TableView
中的单元格相比,您正在尝试对 ListView
中的单元格执行更复杂的操作。在 TableView
中,单元格中显示的对象在变化,因此单元格很容易观察到这一点。在 ListView
中,您希望单元格注意到属于单元格中显示的对象的属性何时发生变化;这是进一步删除的步骤,因此您必须进行一些额外的编码(尽管不多,如您所见)。
您可以创建一个自定义单元工厂来绑定(bind)到 stepNameProperty()
,但这很棘手(您必须确保从 updateItem() 中的旧项目中取消绑定(bind)/删除监听器
方法)。
不过,更简单的方法是使用定义了提取器的 ObservableList
,但没有很好的记录。
首先,修复您的方法名称:您发布的代码中有一些奇怪的不匹配。 getX/setX/xProperty 方法名称应该都正确匹配。 IE。而不是
public void setName(String name) {
stepName.setValue(name);
}
public String getName() {
return stepName.getValue();
}
public StringProperty stepNameProperty() {
return actionStepID;
}
你应该有
public final void setName(String name) {
stepName.setValue(name);
}
public final String getName() {
return stepName.getValue();
}
public StringProperty nameProperty() {
return stepName;
}
其他属性访问器方法也类似。 (显然,字段的名称可以是您喜欢的任何名称,因为它们是私有(private)的。)Making the get/set methods final is good practice.
然后,create the list with an extractor .提取器是一个将列表中的每个元素映射到列表将观察到的 Observable
数组的函数。如果这些值发生变化,它将向列表的观察者触发列表更新。由于您的 ActionStep
的 toString()
方法引用了 nameProperty()
,我假设您希望 ListView
如果 nameProperty()
更改,则更新。所以你想做
listOfSteps = FXCollections.observableArrayList(
actionStep -> new Observable[] { actionStep.nameProperty() } // the "extractor"
);
actionStepsListView.setItems(listOfSteps);
请注意,在 JavaFX 2.2 的早期版本中,ListView
没有正确观察更新事件列表;这是在 Java 8 发布前不久修复的(如果我没记错的话)。(因为你标记了问题 JavaFX8,我假设你使用的是 Java 8,所以你应该没问题。)
如果您不使用 Java 8,则可以使用以下(等效但更冗长)代码:
listOfSteps = FXCollections.observableArrayList(
new Callback<ActionStep, Observable[]>() {
@Override
public Observable[] call(ActionStep actionStep) {
return new Observable[] { actionStep.nameProperty() } ;
}
});
actionStepListView.setItems(listOfSteps);
关于java - ListView 未反射(reflect)更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23822550/