我希望能够单独在ListView中单击一个项目,然后将预先创建的TreeView添加到另一个ListView。 这可能吗?
答案 0 :(得分:2)
通常,将节点直接添加到像ListView或TableView这样的虚拟化控件的项列表中并不是一个好主意。建议的解决方案是将数据项添加到项列表中,并创建将项呈现为节点的单元工厂。
以下是一个示例:
import javafx.application.Application;
import javafx.collections.*;
import javafx.geometry.Insets;
import javafx.scene.Scene;
import javafx.scene.control.*;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import java.io.IOException;
public class TreeViewList extends Application {
private static final int NUM_ITEMS = 10;
@Override
public void start(Stage stage) throws IOException {
ObservableList<TreeItem<String>> items = generateItems();
ListView<TreeItem<String>> sourceListView = createSourceListView(
items
);
ListView<TreeItem<String>> destListView = createDestListView(
sourceListView.getSelectionModel().getSelectedItems()
);
sourceListView.getSelectionModel().selectIndices(
3, 5, 6
);
HBox layout = new HBox(10, sourceListView, destListView);
layout.setPadding(new Insets(10));
stage.setScene(new Scene(layout));
stage.show();
}
private ListView<TreeItem<String>> createSourceListView(
ObservableList<TreeItem<String>> items
) {
ListView<TreeItem<String>> sourceListView = new ListView<>(items);
sourceListView.setCellFactory(param -> new ListCell<TreeItem<String>>() {
@Override
protected void updateItem(TreeItem<String> item, boolean empty) {
super.updateItem(item, empty);
if (empty
|| item == null
|| item.getValue() == null
|| "".equals(item.getValue().trim())) {
setText(null);
return;
}
setText(item.getValue());
}
});
sourceListView.getSelectionModel().setSelectionMode(SelectionMode.MULTIPLE);
return sourceListView;
}
private ListView<TreeItem<String>> createDestListView(
ObservableList<TreeItem<String>> items
) {
ListView<TreeItem<String>> destListView = new ListView<>(
items
);
destListView.setCellFactory(param -> new ListCell<TreeItem<String>>() {
TreeView<String> treeView;
@Override
protected void updateItem(TreeItem<String> item, boolean empty) {
super.updateItem(item, empty);
if (empty
|| item == null
|| item.getValue() == null
|| "".equals(item.getValue().trim())) {
setGraphic(null);
return;
}
if (treeView == null) {
treeView = new TreeView<>();
treeView.setPrefHeight(100);
treeView.setPrefWidth(100);
}
treeView.setRoot(item);
setGraphic(treeView);
}
});
return destListView;
}
private ObservableList<TreeItem<String>> generateItems() {
ObservableList<TreeItem<String>> items = FXCollections.observableArrayList();
for (int i = 0; i < NUM_ITEMS; i++) {
TreeItem<String> nextItem = new TreeItem<>(
"root-" + i
);
for (int j = 0; j < NUM_ITEMS; j++) {
TreeItem<String> childItem = new TreeItem<>(
"child-" + i + "-" +j
);
nextItem.getChildren().add(childItem);
}
items.add(nextItem);
}
return items;
}
public static void main(String[] args) {
launch(args);
}
}
答案 1 :(得分:0)
是的,有可能:
创建第一个TreeView:
TreeItem<String> firstTreeItems = new TreeItem<>("First");
firstTreeItems.setExpanded(false);
for (int i = 1; i < 5; i++) {
TreeItem<String> item = new TreeItem<> ("Item " + i);
firstTreeItems.getChildren().add(item);
}
TreeView<String> firstTree = new TreeView<>(firstTreeItems);
现在是第二个:
TreeItem<String> secondTreeItems = new TreeItem<>("Second");
secondTreeItems.setExpanded(false);
for (int i = 1; i < 5; i++) {
TreeItem<String> item = new TreeItem<> ("Item " + i);
secondTreeItems.getChildren().add(item);
}
TreeView<String> secondTree = new TreeView<>(secondTreeItems);
将它们与字符串一起添加
ListView<Object> list = new ListView<>();
ObservableList<Object> items = FXCollections.observableArrayList ("String", "Another string", firstTree, secondTree);
list.setItems((ObservableList<Object>) items);
答案 2 :(得分:0)
是的,绝对可以!它甚至是官方文档的一部分: https://docs.oracle.com/javase/8/javafx/user-interface-tutorial/tree-table-view.htm
这是它的外观: