是否有方法將模型對象的標識符或模型對象本身存儲在JavaFX中2 TreeItem<String>
?只有Value
來存儲文本...JavaFX 2中的節點的模型標識符2 TreeItem
我從模型對象列表填充TreeView
,並需要找到它時,用戶單擊節點。我習慣在.NET Windows窗體或HTML中使用Value
和Text
,恐怕我不能適應JavaFX的這種思維方式...
是否有方法將模型對象的標識符或模型對象本身存儲在JavaFX中2 TreeItem<String>
?只有Value
來存儲文本...JavaFX 2中的節點的模型標識符2 TreeItem
我從模型對象列表填充TreeView
,並需要找到它時,用戶單擊節點。我習慣在.NET Windows窗體或HTML中使用Value
和Text
,恐怕我不能適應JavaFX的這種思維方式...
您可以使用任何具有TreeView的對象,它們只需要重寫toString()
用於呈現或延伸javafx.scene.Node
例如下節課:
private static class MyObject {
private final String value;
public MyObject(String st) { value = st; }
public String toString() { return "MyObject{" + "value=" + value + '}'; }
}
樹視圖應創建下一個方法:
TreeView<MyObject> treeView = new TreeView<MyObject>();
TreeItem<MyObject> treeRoot = new TreeItem<MyObject>(new MyObject("Root node"));
treeView.setRoot(treeRoot);
我有同樣的問題,因爲OP。另外我想將TreeItem中顯示的值綁定到對象的屬性。這不是完整的,但我正在試驗下面的助手類,在那裏我傳遞了要在TreeItem中引用的「用戶對象」(或項目),以及一個valueProperty(在我的例子中,它是該項的屬性)綁定到TreeItem.value。
final class BoundTreeItem<B, T> extends TreeItem<T> {
public BoundTreeItem(B item, Property<T> valueProperty) {
this(item, valueProperty, null);
}
public BoundTreeItem(B item, Property<T> valueProperty, Node graphic) {
super(null, graphic);
itemProperty.set(item);
this.valueProperty().bindBidirectional(valueProperty);
}
public ObjectProperty<B> itemProperty() {
return itemProperty;
}
public B getItem() {
return itemProperty.get();
}
private ObjectProperty<B> itemProperty = new SimpleObjectProperty<>();
}
這個答案完成嗎?我認爲綁定對於TreeItem的價值來說應該是一個簡單的選項。 – Joel
它適合我。謝謝 。 。 。 –