简体   繁体   English

JavaFX TreeTableView图像伪像

[英]JavaFX TreeTableView Image artefacts

I'd like to add images to each node in a TreeTableView. 我想将图像添加到TreeTableView中的每个节点。 In order to do this, i wrote (as per some pages found with Google) the following: 为了做到这一点,我写了(根据在Google上找到的某些页面)以下内容:

Call to update the view in the views controller 调用以更新视图控制器中的视图

public void refreshView() {
    final List<TreeItem<IUnit>> treeItems = new ArrayList();
    final Collection<IUnit> onRootLevel = source.getUnitsOnRootLevel();
    for (final IUnit unit : onRootLevel) {
        treeItems.add(new MyTreeItem(unit));
    }
    root.getChildren().clear();
    root.getChildren().addAll(treeItems);
}

Each item is wrapped 每个项目都被包裹

public class MyTreeItem extends TreeItem<IUnit> {
    private final IUnit unit;
    public MyTreeItem(final IUnit unit) {
        super(unit, new LevelImage(unit.getLevel().getIcon()).getImage());
        this.unit = unit;
    }
}

Each item gets the image converted from a BufferedImage 每个项目都会从BufferedImage转换图像

public class LevelImage {
    private ImageView imageView;
    public LevelImage(final BufferedImage image) {
        imageView = new ImageView();
        imageView.setFitHeight(16);
        imageView.setFitWidth(16);
        imageView.setVisible(true);
        imageView.setImage(SwingFXUtils.toFXImage(image, null));
    }
    public ImageView getImage() {
        return this.imageView;
    }
}

In principle, this works fine. 原则上,这很好。 Unfortunately when I expand or close the nodes, or refresh the whole table then this leaves artifacts of the images behind or images are not drawn at all anymore. 不幸的是,当我展开或关闭节点或刷新整个表时,这会留下图像的伪像,或者根本不再绘制图像。 Has anybody an Idea whats a possible reason for this might be. 有谁知道这可能是什么原因。 I am Using JavaFX as of Java 10.0.1 我从Java 10.0.1开始使用JavaFX

外观示例

@Itai: Thanks! @Itai:谢谢! But this is only half the answer, it seems. 但这似乎只是答案的一半。 The problem is also with this: 问题也是这样的:

public MyTreeItem(final IUnit unit) {
    super(unit, >> new LevelImage(unit.getLevel().getIcon()).getImage())  <<;
    this.unit = unit;
}

I cannot set the image here, but have to do it in the CellFactory instead. 我无法在此处设置图像,而必须在CellFactory中进行设置。 Otherwise, this does not work. 否则,这将不起作用。 So the correct code here would be 所以这里的正确代码是

public MyTreeItem(final IUnit unit) {
    super(unit);
    this.unit = unit;
}

And have the magic done in the factory, which means, that the first column cannot be just a string anymore, but needs to be the object itself to also get the icon. 并且在工厂中完成了魔术,这意味着第一列不能再只是字符串了,而必须是对象本身才能获得图标。

Anyway, what throw me of course yesterday, was that the TreeTableCell as a private 'updateItem' method with just and 'integer' as parameter. 无论如何,昨天我当然想到的是,TreeTableCell是一个私有的 “ updateItem”方法,带有just和“ integer”作为参数。 Seems my frustration level yesterday got to high and i missed that detail (the parameters). 昨天我的挫败感似乎很高,我错过了这个细节(参数)。 I really thought that the TreeTableCell works differently then all others objects for whatever the reason. 我真的以为TreeTableCell的工作原理与其他所有对象不同,无论出于何种原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM