以编程方式编辑TreeView/TreeItem [英] Programmatically edit TreeView/TreeItem

查看:61
本文介绍了以编程方式编辑TreeView/TreeItem的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

编辑#2 :由于它看起来像是一个错误,因此我已经在

Edit #2: Since it looks like a bug i already posted a bug report in the javaFx-jira. You have to have an account to have access to the issue. I will keep this post up-to-date, if there is new information.

原始帖子: 我有一个带有按钮和TreeView的简单UI.如果按下按钮,则应该在TreeView中添加一个新项目.此项一旦出现在树中,就应该可以对其进行编辑.

Original post: I have a simple UI with a button and a TreeView. If the button gets pressed there should be a new item added to the TreeView. This item should be editable as soon as it appears in the tree.

import javafx.fxml.FXML;
import javafx.scene.control.TreeItem;
import javafx.scene.control.TreeView;
import javafx.scene.control.cell.TextFieldTreeCell;
import javafx.util.converter.DefaultStringConverter;

public class ClientController {

    @FXML
    private TreeView<String> tree;

    public void initialize() {
        tree.setEditable(true);
        tree.setCellFactory(p -> new TextFieldTreeCell<>(new DefaultStringConverter()));

        TreeItem<String> root = new TreeItem<>();
        root.setValue("Items");
        root.setExpanded(true);

        tree.setRoot(root);
    }

    @FXML
    public void createItem() {
        TreeItem<String> newItem = new TreeItem<>();
        newItem.setValue("Item " + tree.getExpandedItemCount());

        tree.getRoot().getChildren().add(newItem);
        tree.requestFocus();
        tree.getSelectionModel().select(newItem);
        tree.edit(newItem);
    }
}

我正在使用的CellFactory是 JavaFX API的一部分.

The CellFactory i am using is part of the JavaFX api.

如果我看一下api文档(

If I have a look at the api-documentation (TreeView#edit) there is not much to do on my site.

我在Google上找到的许多示例,例如为每个TreeItem创建一个上下文菜单.有用,但不是我现在想要的.

Many examples I found via google like this create a context-menu for each TreeItem. Useful, but not exactly what i want, right now.

如果我在UI中选择/双击该项目,则可以编辑任何以前创建的和现有的TreeItem.我想念什么吗?

If I select/double-click the item in the UI I am able to edit any previously created and existing TreeItem. Do I miss something?

编辑#1:

如果createItem方法更改为以下代码:

If the createItem method gets changed to the following code:

@FXML
public void createItem() throws InterruptedException {
    TreeItem<String> newItem = new TreeItem<>();
    newItem.setValue("Item " + this.tree.getExpandedItemCount());

    this.tree.getRoot().getChildren().add(newItem);
    this.tree.requestFocus();
    this.tree.getSelectionModel().select(newItem);
    Thread.sleep(100);
    Platform.runLater(new Runnable() {
        @Override
        public void run() {
            SimpleController.this.tree.edit(newItem);
        }
    });
}

每个新项目均已正确标记为可编辑(已选择创建的文本字段及其内容). 使用 Platform.runLater没有 Thread.sleep 不起作用,没有 runLater Thread.sleep 都不起作用.

every new item is correctly marked for edit (the created textfield and its content are selected). Using Platform.runLater without Thread.sleep doesnt work and Thread.sleep without runLater neither.

这根本感觉不对.我怎么了

This simply does not feel right. What is my problem?

我提供了一个非常小的示例(日食)项目: https://www.dropbox.com/s/duos0ynw4rqp3yn/Test.zip ?dl = 0 包含主要方法,FXML文件和问题"控制器.

I provide a very small example (eclipse) project: https://www.dropbox.com/s/duos0ynw4rqp3yn/Test.zip?dl=0 containing The main-method, the FXML-File and the "problematic" controller.

推荐答案

TreeCells(即它们的内容和对treeItem的绑定)在布局遍历中延迟更新,这在下一个脉冲触发时非常迟".在该脉冲之前或之后,可能会发生Platform.runLater(..)或任何硬编码的延迟,这就是(可能)为什么两者似乎都正常工作或不正常工作的原因.

TreeCells (that is their content and binding to the treeItem) are updated lazily in a layout pass, that is very "late" when the next pulse is fired. A Platform.runLater(..) or any hard-coded delay may happen before or after that pulse, that's (probably) why both seem to work or not spuriously.

因此,另一个怪异的技巧是在添加新项目之后并以编程方式开始编辑之前,手动在树上强制重新布局:

So another whacky hack is to manually force a re-layout on the tree after having added a new item and before programatically starting an edit:

root.getChildren().add(newItem);
tree.layout();
tree.edit(newItem);

无需赘言,这不是必须的-当前行为是一个严重的错误,必须立即修复(...表示... jdk 9)

Needless to say, that this shouldn't be necessary - the current behaviour is a severe bug and must be fixed at once (... meaning ... jdk 9)

这篇关于以编程方式编辑TreeView/TreeItem的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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