如何使 TreeView 中的 TreeItem 确认鼠标单击事件? [英] How do I make a mouse click event be acknowledged by a TreeItem in a TreeView?

查看:25
本文介绍了如何使 TreeView 中的 TreeItem 确认鼠标单击事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fxml文件如下(头文件省略):

The fxml file is as follows (headers omitted):

<BorderPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity"
    minWidth="-Infinity" prefHeight="600.0" prefWidth="800.0"
    xmlns="http://javafx.com/javafx/8" xmlns:fx="http://javafx.com/fxml/1"
    fx:id="pane"
    fx:controller="com.github.parboiled1.grappa.debugger.mainwindow.MainWindowUi">
    <top>
        <MenuBar BorderPane.alignment="CENTER">
            <Menu mnemonicParsing="false" text="File">
                <MenuItem fx:id="loadInput" mnemonicParsing="false"
                    text="Load file" onAction="#loadFileEvent"/>
                <MenuItem fx:id="parse" mnemonicParsing="false"
                    text="Parse" onAction="#parseEvent"/>
                <MenuItem fx:id="closeButton" mnemonicParsing="false"
                    text="Close" onAction="#closeWindowEvent"/>
            </Menu>
        </MenuBar>
    </top>
    <center>
        <SplitPane dividerPositions="0.5" prefHeight="160.0" prefWidth="200.0"
            BorderPane.alignment="CENTER">
            <SplitPane dividerPositions="0.5" orientation="VERTICAL">
                <TreeView fx:id="traceTree" prefHeight="200.0"
                    prefWidth="200.0" editable="false"/>
                <TextArea fx:id="traceDetail" prefHeight="200.0"
                    prefWidth="200.0"/>
            </SplitPane>
            <TextArea fx:id="inputText" prefHeight="200.0" prefWidth="200.0"/>
        </SplitPane>
    </center>
</BorderPane>

我可以毫无问题地设置 TreeView 的根.树更新没有问题.

I can set the root of the TreeView with no problem at all. The tree is updated with no problem.

我遇到的问题是我无法在视图中的给定项目上触发事件.我尝试添加了一个 onMouseClicked 事件和一个简单的 System.out.println() 并且我可以看到正在触发的事件,无论我在树中单击哪个项目.但是我根本无法获得在视图中被点击的项目.

The problem I have is that I cannot manage to have an event fired on a given item in the view. I tried and added a onMouseClicked event with a simple System.out.println() and I can see the event being fired, whichever item I click in the tree. But I cannot manage to get the item which has been clicked in the view at all.

我该怎么做?

推荐答案

使用单元工厂为每个树单元注册一个鼠标监听器.我不知道 TreeView 中的数据类型,但如果它是 String,它可能看起来像这样:

Register a mouse listener with each tree cell, using a cell factory. I don't know the data type you have in your TreeView, but if it were String it might look something like this:

// Controller class:
public class MainWindowUi {

    @FXML
    private TreeView<String> traceTree ;

    // ...

    public void initialize() {
        traceTree.setCellFactory(tree -> {
            TreeCell<String> cell = new TreeCell<String>() {
                @Override
                public void updateItem(String item, boolean empty) {
                    super.updateItem(item, empty) ;
                    if (empty) {
                        setText(null);
                    } else {
                        setText(item);
                    }
                }
            };
            cell.setOnMouseClicked(event -> {
                if (! cell.isEmpty()) {
                    TreeItem<String> treeItem = cell.getTreeItem();
                    // do whatever you need with the treeItem...
                }
            });
            return cell ;
        });
    }

    // ... 
}

这篇关于如何使 TreeView 中的 TreeItem 确认鼠标单击事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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