将SWT进度条切换为不确定(替换小部件)

时间:2013-10-15 11:10:45

标签: java user-interface swt progress-bar

似乎无法将单个SWT进度条小部件的状态从确定切换到不确定(SWT.INDETERMINATE)并返回。 我想基于用户交互这样做,这可能是经常的,因此处置和重新创建可能不是最好的主意。

我尝试的解决方案是隐藏确定条并显示不确定条并强制重新布局父组合。

但是,通过执行此操作,条形图不会重新出现在同一位置,表示隐藏元素占用其空间。是否可以进行就地替换?

2 个答案:

答案 0 :(得分:3)

here。使用StackLayout和两个ProgressBar实例。或者,使用带有#setIndeterminate(boolean)

的JProgessBar

答案 1 :(得分:0)

您可以简单地将ProgressBar包裹在Composite中,然后将dispose()包裹在您想要更改它的位置并创建一个具有您想要的样式的新ProgressBar: / p>

private static ProgressBar  bar;
private static boolean indeterminate = false;

public static void main(String[] args)
{
    final Display display = Display.getDefault();
    final Shell shell = new Shell(display);
    shell.setLayout(new GridLayout(1, false));

    new Button(shell, SWT.PUSH).setText("Above");

    final Composite barParent = new Composite(shell, SWT.NONE);
    barParent.setLayout(new GridLayout(1, false));
    barParent.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));

    bar = new ProgressBar(barParent, SWT.SMOOTH);
    bar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
    bar.setMaximum(10);
    bar.setSelection(5);

    new Button(shell, SWT.PUSH).setText("Below");

    final Runnable timer = new Runnable()
    {
        public void run()
        {
            if (bar.isDisposed())
                return;

            indeterminate = !indeterminate;

            bar.dispose();
            bar = new ProgressBar(barParent, indeterminate ? SWT.INDETERMINATE : SWT.SMOOTH);
            bar.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true));
            bar.setMaximum(10);
            bar.setSelection(5);
            barParent.layout();

            display.timerExec(2000, this);
        }
    };
    display.timerExec(2000, timer);

    shell.pack();
    shell.open();

    while (!shell.isDisposed())
    {
        if (!display.readAndDispatch())
            display.sleep();
    }
    display.dispose();
}

不确定

enter image description here

平滑:

enter image description here