java - 将元素添加到模型时,JFace TreeViewer 不会更新

标签 java java-8 eclipse-plugin swt jface

我知道这有几个答案,但没有一个对我有用。此外,问题也有点不同,因为我使用的是自己的模型实现。

此代码显示了单击按钮时应该发生的情况。

这里的内容设置为树查看器:

root = new TreeRootModel(solverEntries, null, configParser.getConfFile());
root.addInstances(instances);
viewer.setInput(new TreeRootModel[] { root });
column.pack(); // important to see TreeViewer at start
viewer.expandAll();

TreeRootModel类只是一个存储类,其下存储一个数组,有getter和setter。不值得在这里展示。

ToolBar toolBar = new ToolBar(newMainComposite, SWT.FLAT | SWT.WRAP | SWT.RIGHT);
ToolItem addItem = new ToolItem(toolBar, SWT.PUSH);
addItem.setText("Add");
addItem.setImage(addIcon);

addItem.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(SelectionEvent e) {
        InstanceModel newInstance = new InstanceModel("new Instance", null);
        instances.add(newInstance);
        // none of these three calls has an effect
        viewer.refresh();
        tree.update();
        column.pack();
    }
});

所以TreeViewer的内容是根,根包含List“实例”。这些对象将显示在 TreeViewer 中。单击添加按钮后,一个对象将添加到 List 实例中。我想立即在 TreeViewer 中看到它。

ContentProvider 的 inputChanged 方法为空。

public class GoalInstanceContentProvider implements ITreeContentProvider, IResourceChangeListener {

    TreeRootModel root;
    ArrayList<InstanceModel> instances = new ArrayList<>();
    private TreeViewer viewer;
    private TreeRootModel[] input;

    @Override
    public void dispose() {
    }

    @Override
    public void inputChanged(Viewer viewer, Object oldInput, Object newInput) {
    }

    @SuppressWarnings("unchecked")
    @Override
    public Object[] getElements(Object inputElement) {
        if (inputElement instanceof TreeRootModel) {
            root = (TreeRootModel) inputElement;
            return root.getInstances().toArray();
        } else if (inputElement instanceof ArrayList) {
            instances = (ArrayList<InstanceModel>) inputElement;
            return instances.toArray();
        }
        return ArrayContentProvider.getInstance().getElements(inputElement);
    }
    ...
}

最佳答案

设置输入时,停止将 root 放入数组中。直接使用。

关于java - 将元素添加到模型时,JFace TreeViewer 不会更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56294938/

相关文章:

java - 将 CQ5 组件设置为可编辑或不可编辑

java - 如何使用基于Xtext的语言?

java - Eclipse:Java 构建路径重置中的 JRE 系统库

java - SWTBot 找不到文本为 : Dynamic Web Project 的节点

java - 在Eclipse插件中打开文件到某一行

java Spark框架无法设置或读取cookie

java - 为什么 .bat 文件会跳行并跳到末尾?

java - jqgrid 只读组合框

java - 聚合函数和行为 - 并行流

java - 将旧式 if 用法转换为Optional.ifPresent() 时出现问题