如何给动态加载TreeViewItem一个EventHandler? [英] How to give an dynamicly loaded TreeViewItem an EventHandler?

查看:217
本文介绍了如何给动态加载TreeViewItem一个EventHandler?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我编写了一个基于数据库的聊天系统。
每个用户的朋友列表在登录后在TreeView中获取加载项。



表示:
登录后,我请求使用者的姓名通过以下Funktion,
String namesSt [] = get.getUserFriendNameByUserID(currentUserID);



使用给定的名称将它们作为TreeItem加载到我的Friendlist / TreeRootItemrootItem

  for(int counter = 0; counter< ; namesSt.length; counter ++){
System.out.println(namesSt [counter]);
TreeItem< String> item = new TreeItem< String> (namesSt [计数器]);

item.addEventHandler(MouseEvent.MOUSE_CLICKED,handler);

rootItem.getChildren()。add(item);
}

当我添加我的rootItem时,我看到了TreeView中的Names。
但是如果我点击一个名字,给定的MouseEventHandler不会被调用。



此外,我只想请求触发元素的元素的文本MouseEvent,以便我可以将这些名称提交到一个特殊的功能。



我如何识别这样的一个MouseEvent?
如何从动态创建的TreeItem中调用它?



感谢您的任何帮助:)



cheerse
Tobi

解决方案

TreeItem 表示数据,而不是UI组件。所以他们不会生成鼠标事件。您需要在 TreeCell 上注册鼠标监听器。为此,请在 TreeView 上设置单元格工厂。单元格工厂是根据需要创建 TreeCell 的函数。因此,这将适用于动态添加的树项目。



您将需要这样的东西:

 的TreeView<字符串>树视图 ; 

// ...

treeView.setCellFactory(tv - > {
TreeCell< String> cell = new TreeCell<>();
cell.textProperty()。bind(cell.itemProperty());
cell.addEventHandler(MouseEvent.MOUSE_CLICKED,event - > {
if(!cell.isEmpty()){
String value = cell.getItem();
TreeItem< String> treeItem = cell.getTreeItem(); //如果需要
//进程...
}
});
返回单元格;
}


at the moment i programm a database based Chat System. The friendlist of every User gets loadet in a TreeView after the login.

means: After the login I request the names of the useres friends by the following Funktion, String namesSt[] = get.getUserFriendNameByUserID(currentUserID);

To use the given Names to load them as TreeItem into my Friendlist / TreeRootItem "rootItem"

for (int counter = 0; counter < namesSt.length; counter++) {
        System.out.println(namesSt[counter]);
        TreeItem<String> item = new TreeItem<String> (namesSt[counter]);

        item.addEventHandler(MouseEvent.MOUSE_CLICKED,handler);

        rootItem.getChildren().add(item);
    }

When I now add my rootItem, I see the Names in the TreeView. But if I click on a name, the given MouseEventHandler doesn´t get called.

Further I just want to request the text of the Element which trigger the MouseEvent, so that i can submit these name to a spezial funktion.

How can i realice such an MouseEvent? How is it possible to call it from the dynamicly created TreeItem?

Thank you for any help :)

cheerse Tobi

解决方案

TreeItems represent the data, not the UI component. So they don't generate mouse events. You need to register the mouse listener on the TreeCell. To do this, set a cell factory on the TreeView. The cell factory is a function that creates TreeCells as they are needed. Thus this will work for dynamically added tree items too.

You will need something like this:

TreeView<String> treeView ;

// ...

treeView.setCellFactory( tv -> {
    TreeCell<String> cell = new TreeCell<>();
    cell.textProperty().bind(cell.itemProperty());
    cell.addEventHandler(MouseEvent.MOUSE_CLICKED, event -> {
        if (! cell.isEmpty()) {
            String value = cell.getItem();
            TreeItem<String> treeItem = cell.getTreeItem(); // if needed
            // process ...
        }
    });
    return cell ;
}

这篇关于如何给动态加载TreeViewItem一个EventHandler?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆