我想在树根节点(JavaFX)中单击右键菜单选项。任何人都可以帮助我。
TreeItem<String> root = new TreeItem<>(""+selectedDirectory);
root.setExpanded(true);
locationTreeView.setRoot(root);
root.getChildren().addAll(
new TreeItem<>("Item 1"),
new TreeItem<>("Item 2"),
new TreeItem<>("Item 3")
);
答案 0 :(得分:8)
您可以分两步执行所需的行为:
TreeCell
; TreeView
工厂
TreeCell
上附加上下文菜单。以下代码定义了自定义TreeCell
工厂:
// defines a custom tree cell factory for the tree view
tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
@Override
public TreeCell<String> call(TreeView<String> arg0) {
// custom tree cell that defines a context menu for the root tree item
return new MyTreeCell();
}
});
并且,这是一个自定义树单元格的实现,它附加了根树项目的上下文菜单:
class MyTreeCell extends TextFieldTreeCell<String> {
private ContextMenu rootContextMenu;
public MyTreeCell() {
// instantiate the root context menu
rootContextMenu =
ContextMenuBuilder.create()
.items(
MenuItemBuilder.create()
.text("Menu Item")
.onAction(
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
System.out.println("Menu Item Clicked!");
}
}
)
.build()
)
.build();
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// if the item is not empty and is a root...
if (!empty && getTreeItem().getParent() == null) {
setContextMenu(rootContextMenu);
}
}
}
以下示例说明了单元格工厂和自定义单元格的使用:
public class TreeViewWithContextMenuOnRoot extends Application {
public static void main(String[] args) {
launch(args);
}
@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("Tree with context menu on root");
TreeItem<String> rootItem = new TreeItem<String> ("Tree root");
rootItem.setExpanded(true);
for (int i = 1; i < 3; i++) {
TreeItem<String> item = new TreeItem<String> ("item" + i);
rootItem.getChildren().add(item);
}
final TreeView<String> tree = new TreeView<String> ();
tree.setRoot(rootItem);
// defines a custom tree cell factory for the tree view
tree.setCellFactory(new Callback<TreeView<String>, TreeCell<String>>() {
@Override
public TreeCell<String> call(TreeView<String> arg0) {
// custom tree cell that defines a context menu for the root tree item
return new MyTreeCell();
}
});
StackPane root = new StackPane();
root.getChildren().add(tree);
primaryStage.setScene(new Scene(root, 200, 100));
primaryStage.show();
}
private static class MyTreeCell extends TextFieldTreeCell<String> {
private ContextMenu rootContextMenu;
public MyTreeCell() {
// instantiate the root context menu
rootContextMenu =
ContextMenuBuilder.create()
.items(
MenuItemBuilder.create()
.text("Menu Item")
.onAction(
new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent arg0) {
System.out.println("Menu Item Clicked!");
}
}
)
.build()
)
.build();
}
@Override
public void updateItem(String item, boolean empty) {
super.updateItem(item, empty);
// if the item is not empty and is a root...
if (!empty && getTreeItem().getParent() == null) {
setContextMenu(rootContextMenu);
}
}
}
}
您可以查看TreeView
tutorial以查看与此JavaFX控件相关的其他用法和示例。