在树根节点中右键单击菜单选项 [英] right click menu option in the tree root node
本文介绍了在树根节点中右键单击菜单选项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想要右键单击树根节点(JavaFX)中的菜单选项.谁能帮我这个忙.
I want right click menu option in the tree root node(JavaFX). Could any one help me on this.
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")
);
推荐答案
您可以通过两个步骤执行所需的行为:
You can perform the desired behaviour in two steps:
- 在
TreeView
上定义自定义TreeCell
工厂; - 在根树项目的
TreeCell
上附加上下文菜单.
- Defining a custom
TreeCell
factory on yourTreeView
; - Attaching a context menu on the
TreeCell
of the root tree item.
以下代码定义了自定义TreeCell
工厂:
The following code defines the custom TreeCell
factory:
// 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();
}
});
并且,这是一个自定义树单元的实现,该树单元为根树项附加了上下文菜单:
And, here is the implementation of a custom tree cell that attaches a context menu for the root tree item:
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);
}
}
}
以下示例说明了单元工厂和自定义单元一起使用的情况:
The following example ilustrates the use of both, cell factory and custom cell, together:
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
教程查看与此JavaFX控件相关的其他用法和示例.
You can take a look at the TreeView
tutorial to see other uses and examples related to this JavaFX control.
这篇关于在树根节点中右键单击菜单选项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文