Javafx Treeview 项目操作事件 [英] Javafx Treeview item action event
问题描述
我正在尝试使用 treeView 创建菜单.这是我第一次使用 treeView 并且已经在几个网站上阅读了它.
I'm trying to create a menu using a treeView. This is the first time I'm using treeView and have been reading up on it on several websites.
我在操作事件方面遇到了一些问题.我想要做的基本上是在用户单击树视图中的节点时触发和事件到目前为止我有以下内容:
I'm having some problems when it comes to action event. What I want to do is basically to fire and event when ever the user clicks a node in the treeview so far I have the following:
TreeItem<String> rootItem = new TreeItem<String>("Navigation");
TreeItem<String> statistics = new TreeItem<String>("Statistics");
TreeItem<String> clan = new TreeItem<String>("Clan page");
clan.addEventHandler(MouseEvent, new EventHandler<MouseEvent>() {
@Override
public void handle(MouseEvent event) {
// TODO Auto-generated method stub
}
});
rootItem.getChildren().add(statistics);
rootItem.getChildren().add(clan);
TreeView<String> tree = new TreeView<String>(rootItem);
遗憾的是,这似乎不起作用.
Sadly this doesn't seem to work.
有什么方法可以在不更改 的情况下向
输入 treeView
中的各个项目添加 clicklistener
甚至 actionlistener
treeItemsButton
?
Is there any way I can add a clicklistener
or even an actionlistener
to the individual items in my treeView
without changing the treeItems
to type Button
?
推荐答案
根据JavaFX 2.2 文档 :
" ..a TreeItem 不是一个节点,因此不会有任何视觉事件在 TreeItem 上触发,要获取这些事件,需要添加相关的观察者TreeCell 实例(通过自定义单元工厂)."
" ..a TreeItem is not a Node, and therefore no visual events will be fired on the TreeItem, To get these events, it is necessary to add relevant observers to the TreeCell instances (via a custom cell factory)."
我认为这个 example 关于使用 TreeView
会以某种方式有用.
I think this example on using TreeView
will be somehow useful.
这篇关于Javafx Treeview 项目操作事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!