Java SWT模式进度条松动窗口焦点

Java SWT模式进度条松动窗口焦点,java,swt,Java,Swt,我有一个模态对话框,用线程生成任务。 这工作得很好,除了主窗口不再可聚焦外,在操作系统任务栏中单击“我的应用程序”图标没有任何效果,这种类型的事件被冻结,当其他窗口结束时,这是一个问题,我想再次显示它 (我只使用SWT,没有JFace) 有什么想法吗?我认为你的问题是这两者的结合: 您有两个事件循环(while(!X.isDisposed())部分) 您正在通过在主线程上使用Thread.sleep()阻塞UI 第2点导致应用程序在显示进度条时对用户交互没有响应,例如,用户无法再聚焦窗口 还要注

我有一个模态对话框,用线程生成任务。 这工作得很好,除了主窗口不再可聚焦外,在操作系统任务栏中单击“我的应用程序”图标没有任何效果,这种类型的事件被冻结,当其他窗口结束时,这是一个问题,我想再次显示它

(我只使用SWT,没有JFace)


有什么想法吗?

我认为你的问题是这两者的结合:

  • 您有两个事件循环(while(!X.isDisposed())部分)
  • 您正在通过在主线程上使用
    Thread.sleep()
    阻塞UI
  • 第2点导致应用程序在显示进度条时对用户交互没有响应,例如,用户无法再聚焦窗口

    还要注意的是,(至少对于您目前掌握的代码而言,
    对话框是不必要的。我附上了一个代码示例,演示了如何做两件事:

  • 在主窗口的顶部有一个模态窗口
  • 使用
    Display#timerExec(int,Runnable)
  • 受保护的进度条;
    保护壳;
    public ProgressBox(Shell父级,最终字符串名)
    {
    this.shell=新shell(父级,SWT.APPLICATION_MODAL | SWT.TITLE);
    setLayout(新的FillLayout());
    bar=新的ProgressBar(外壳,SWT.光滑);
    shell.setText(名称);
    shell.pack();
    shell.open();
    }
    公开作废
    {
    最终int最大值=10;
    此.bar.setMaximum(最大值);
    新线程(newrunnable())
    {
    @凌驾
    公开募捐
    {
    
    对于(int i=1;我可以请你发布一个完整的示例吗?这样我们就可以复制/粘贴它,直接看看问题出在哪里?还有你使用的是什么操作系统?我更新了,现在可以直接测试了。我在Windows 7上感谢这些信息,这可能会让我走上正轨。问题是我有一个有序的任务列表要执行,这就是为什么我有一个loo因此,如果我在你的例子中替换timerExec,我会遇到同样的问题,或者没有响应对话框problem@Gnervey然后,您应该启动一个新线程,只在asyncExec调用中包装实际的UI交互。@n当然,您可能想看看
    ProgressMonitorDialog
    。这是一个相关的问题@Gnervey我已经更新了我的代码,使用非UI-
    线程
    ,并且只在
    asynceec
    调用中包装UI交互。
    import org.eclipse.swt.SWT;
    import org.eclipse.swt.layout.FillLayout;
    import org.eclipse.swt.widgets.Dialog;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.ProgressBar;
    import org.eclipse.swt.widgets.Shell;
    
    public class ProgressBox extends Dialog {
    
        protected Shell dialog;
        protected ProgressBar bar;
    
        public ProgressBox(Shell parent, final String name) {
            super(parent);
            dialog = new Shell(parent, SWT.APPLICATION_MODAL | SWT.TITLE); 
            dialog.setSize(400,64);
            dialog.setText(name);
            dialog.setLayout(new FillLayout());
            bar = new ProgressBar(dialog, SWT.SMOOTH);
        }
    
        public void playScript() {
            this.bar.setMaximum(10);
    
            this.dialog.open();
    
            final Display display = dialog.getDisplay();
            display.asyncExec(new Runnable() { //asyncExec syncExec
                @Override
                public void run() {
                    try {
                        for (int i=0; i<10; i++) {
                            try {
                                Thread.sleep(1000); // To simulate a task execution
                            } catch (InterruptedException e) {}
                            bar.setSelection(i+1);
                            display.sleep();
                        }
                    } finally {
                        dialog.close();
                    }
                }
            });
            while (!dialog.isDisposed()) { 
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        }
    }
    
    import org.eclipse.swt.widgets.Display;
    
    public class MainWindow {
    
        protected Shell shell;
    
        public static void main(String[] args) {
            try {
                MainWindow window = new MainWindow();
                window.open();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        public void open() {
            Display display = Display.getDefault();
            shell = new Shell();
            shell.setSize(450, 300);
            shell.open();
            shell.layout();
    
            ProgressBox progressBox = new ProgressBox(shell, "Test");
            progressBox.playScript();
    
            while (!shell.isDisposed()) {
                if (!display.readAndDispatch()) {
                    display.sleep();
                }
            }
        }
    
    }
    
    protected ProgressBar bar;
    protected Shell       shell;
    
    public ProgressBox(Shell parent, final String name)
    {
        this.shell = new Shell(parent, SWT.APPLICATION_MODAL | SWT.TITLE);
        shell.setLayout(new FillLayout());
    
        bar = new ProgressBar(shell, SWT.SMOOTH);
    
        shell.setText(name);
        shell.pack();
        shell.open();
    }
    
    public void open()
    {
        final int max = 10;
    
        this.bar.setMaximum(max);
    
        new Thread(new Runnable()
        {
            @Override
            public void run()
            {
                for (int i = 1; i <= max; i++)
                {
                    if (bar == null || bar.isDisposed())
                    {
                        closeShell();
                        return;
                    }
                    else
                    {
                        final int index = i;
                        shell.getDisplay().asyncExec(new Runnable()
                        {
                            @Override
                            public void run()
                            {
                                bar.setSelection(index);
                            }
                        });
                    }
    
                    try
                    {
                        Thread.sleep(1000);
                    }
                    catch (InterruptedException e)
                    {
                        closeShell();
                        e.printStackTrace();
                    }
                }
    
                closeShell();
            }
    
            private void closeShell()
            {
                shell.getDisplay().asyncExec(new Runnable()
                {
                    @Override
                    public void run()
                    {
                        shell.close();
                    }
                });
            }
        }).start();
    }
    
    public static void main(String[] args)
    {
        Display display = Display.getDefault();
        Shell shell = new Shell();
        shell.setSize(450, 300);
        shell.open();
    
        new ProgressBox(shell, "Progress").open();
    
        while (!shell.isDisposed())
        {
            if (!display.readAndDispatch())
            {
                display.sleep();
            }
        }
    }