如何使某些JavaFX TreeView节点不可选择? [英] How to make certain JavaFX TreeView nodes non-selectable?

查看:188
本文介绍了如何使某些JavaFX TreeView节点不可选择?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使JavaFX TreeView中的文件夹"节点可扩展和折叠,但不可选择.

我找到了此讨论,并调查了EventFilter,但似乎没有与TreeView选择相对应的任何EventType都会更改.第二个建议是自定义选择模型,这听起来像是对我的深入了解.因此,我是否坚持允许选择事件触发我的侦听器,然后对其中的垃圾进行分类?

解决方案

有点hacky,但是我最终还是这样:

table.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
    if (newValue != null && !newValue.isLeaf()) {
        Platform.runLater(() -> table.getSelectionModel().clearSelection());
    }
});

对我来说,单击非叶节点时只需清除选择就足够了.但是,重新选择oldValue参数并不难,但要知道这将再次触发更改事件(clearSelection调用也是如此,这就是为什么newValue != null检查是必需的). /p>

I would like to make the 'folder' nodes in my JavaFX TreeView expandable and collapsible but not selectable.

I found this discussion and looked into EventFilter, but there does not appear to be any EventType that corresponds with TreeView selection changes. The second suggestion, a custom selection model, sounds like a deep dive to me. So, am I stuck allowing the selection events to trigger my listener and then sort through the trash there?

解决方案

It's a bit hacky, but I ended up doing it like this:

table.getSelectionModel().selectedItemProperty().addListener((observable, oldValue, newValue) -> {
    if (newValue != null && !newValue.isLeaf()) {
        Platform.runLater(() -> table.getSelectionModel().clearSelection());
    }
});

For me it was enough to just clear the selection when clicking a non-leaf node. However, it shouldn't be to hard to just reselect the oldValue parameter, but be aware that this will fire a change event again (so does the clearSelection call, that's why the newValue != null check is necessary).

这篇关于如何使某些JavaFX TreeView节点不可选择?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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