问题描述:

Can someone explain why javadoc for TreeItem uses FXCollections.observableArrayList to create the local children variable, rather than ArrayList e.g.

private ObservableList<TreeItem<File>> buildChildren(TreeItem<File> TreeItem) {

File f = TreeItem.getValue();

if (f != null && f.isDirectory()) {

File[] files = f.listFiles();

if (files != null) {

ObservableList<TreeItem<File>> children = FXCollections.observableArrayList();

for (File childFile : files) {

children.add(createNode(childFile));

}

return children;

}

}

return FXCollections.emptyObservableList();

}

The returned collection is then added as follows

@Override public ObservableList<TreeItem<File>> getChildren() {

if (isFirstTimeChildren) {

isFirstTimeChildren = false;

// First getChildren() call, so we actually go off and

// determine the children of the File contained in this TreeItem.

super.getChildren().setAll(buildChildren(this));

}

return super.getChildren();

}

Since super.getChildren() is already an ObservableList, what is the advantage of adding an ObservableList to an ObservableList. In my code I created the child items using ArrayList and added a valueChangedListener on the root node and received the event when setting value on a child item.

The javadoc for TreeView on the other hand add TreeItem elements separately and does not wrap them in ObservableList.

Is there something special going on here?

相关阅读:
Top