Java TreeEditor UI错误

时间:2017-05-10 03:44:26

标签: java tree swt progress-bar

目前我在使用TreeEditor时遇到小错误。我的代码:

package test;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.TreeEditor;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.ProgressBar;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeColumn;
import org.eclipse.swt.widgets.TreeItem;

public class Test {
    public static void main(String[] args) {
        Display display = new Display();
        final Shell shell = new Shell(display);
        shell.setLayout(new FillLayout());
        Tree tree = new Tree(shell, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
        tree.setHeaderVisible(true);
        TreeColumn column1 = new TreeColumn(tree, SWT.LEFT);
        column1.setText("Column 1");
        column1.setWidth(200);
        TreeColumn column2 = new TreeColumn(tree, SWT.CENTER);
        column2.setText("Column 2");
        column2.setWidth(200);
        TreeColumn column3 = new TreeColumn(tree, SWT.RIGHT);
        column3.setText("Column 3");
        column3.setWidth(200);
        TreeColumn column4 = new TreeColumn(tree, SWT.LEFT);
        column4.setText("Column 4");
        column4.setWidth(200);

        for (int i = 0; i < 20; i++) {
            TreeItem item = new TreeItem(tree, SWT.NONE);
            item.setText(new String[] { "item " + i, "abc" + i, "percentage " + i });
            ProgressBar bar = new ProgressBar(tree, SWT.NULL);
            bar.setVisible(true);
            bar.setSelection(i);
            bar.setToolTipText(i + "%");
            TreeEditor editor = new TreeEditor(tree);
            editor.grabHorizontal = true;
            editor.grabVertical = true;
            editor.setEditor(bar, item, 3);

        }
        // shell.pack();
        shell.setSize(900, 295);
        shell.open();
        while (!shell.isDisposed()) {
            if (!display.readAndDispatch()) {
                display.sleep();
            }
        }
        display.dispose();
    }
}

当我点击底行文本(“第12项”)时,操作系统会自动向下滚动1行。但是当我检查ProgressBar ToolTip时,“item 12”进度条显示11%。这意味着进度条不会向下滚动一行。 我该如何解决这个问题?谢谢。

1 个答案:

答案 0 :(得分:1)

对我来说,整个列(使用ControlEditor进行设置时)不会与其他列一起自动滚动。可以在此处报告错误:Eclipse Bugzilla

它似乎不是ProgressBar的问题。例如,如果将其替换为Label,则会遇到同样的问题。

似乎有效的一种解决方法是在侦听器中调用setSelection(TreeItem[])方法。这似乎让专栏得以纠正。

例如:

tree.addSelectionListener(new SelectionAdapter() {
    @Override
    public void widgetSelected(final SelectionEvent e) {
        tree.setSelection(tree.getSelection());
    }
});

获取所选TreeItem然后立即设置它们应该是相当无害的,只要您的应用程序在SelectionEvent Tree周围没有做任何特殊操作。