SWT 强制重新绘制标签

发布于 2024-12-02 10:16:24 字数 1245 浏览 0 评论 0原文

我有一个简单的方法来设置标签的文本:

public void setStatus(final String status)
{
   statusLabel.setText(status);
}

当我在任何类型的 Display.asyncExec(Runnable) 之前调用它时,它似乎直到 Runnable 之后才执行> 已完成。我什至尝试在 Runnable 中实现它,但它不起作用。例如,我有一个“加载文件”菜单项,我希望在用户选择文件后状态显示“正在加载:[文件名]”:

    MenuItem mntmLoadFile = new MenuItem(menu_4, SWT.NONE);
    mntmLoadFile.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent arg0) {
            String file = GuiWorkshop.selectFile(shell, new String[]{"*.apk"});
            if(file != null){
                try{
                    setStatus("Loading File: " + file);
                    controller.loadFile(new File(file));
                } catch (Exception e){
                    GuiWorkshop.messageError(shell, "Could not load file: " + e.getMessage());
                    logger.error("Could not load file: ", e);
                }
                clearStatus();
            }
        }
    });
    mntmLoadFile.setText("Load file");

controller.loadFile(new File(file)); 执行所有加载,但 setStatus 在完成之前永远不会更新。有没有办法在执行下一行之前强制完成它?我不确定这是本地线程问题还是 SWT 线程问题。

I have a simple method that sets the text of a label:

public void setStatus(final String status)
{
   statusLabel.setText(status);
}

When I call it before any sort of Display.asyncExec(Runnable), it seems to not execute until after that Runnable has completed. I have even tried to implement it in the Runnable, and it doesn't work. For example, I have a 'load file' menu item that I would like the status to display "Loading: [filename]" after a user selects a file:

    MenuItem mntmLoadFile = new MenuItem(menu_4, SWT.NONE);
    mntmLoadFile.addSelectionListener(new SelectionAdapter() {
        @Override
        public void widgetSelected(SelectionEvent arg0) {
            String file = GuiWorkshop.selectFile(shell, new String[]{"*.apk"});
            if(file != null){
                try{
                    setStatus("Loading File: " + file);
                    controller.loadFile(new File(file));
                } catch (Exception e){
                    GuiWorkshop.messageError(shell, "Could not load file: " + e.getMessage());
                    logger.error("Could not load file: ", e);
                }
                clearStatus();
            }
        }
    });
    mntmLoadFile.setText("Load file");

The line controller.loadFile(new File(file)); is what performs all of the loading, but the setStatus will never update until it has completed. Is there a way to force that to finish, prior to the next line executing? I am not sure if this is a local thread problem, or an SWT thread problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

有深☉意 2024-12-09 10:16:24

您可以循环调用 Display.readAndDispatch(),直到返回 false。这将确保所有重绘操作都已完成。

...
setStatus("Loading File: " + file);

while (display.readAndDispatch())
{
   // nothing to do
}

controller.loadFile(new File(file));
...

注意:您可能不想在 UI 线程上调用 controller.loadFile()。这将导致 UI 挂起。相反,您应该在后台线程中调用 controller.loadFile()。当后台线程完成时,让 UI 线程更新状态。

You can call Display.readAndDispatch() in a loop until it returns false. This will ensure all redraw operations have completed.

...
setStatus("Loading File: " + file);

while (display.readAndDispatch())
{
   // nothing to do
}

controller.loadFile(new File(file));
...

Note: You probably do not want to call controller.loadFile() on the UI thread. This will cause the UI to hang. Instead, you should call controller.loadFile() in a background thread. When the background thread finishes, have the UI thread update the status.

甜妞爱困 2024-12-09 10:16:24

设置文本后必须刷新标签。 Control API(它是所有 SWT 组件的超类)有一个方法 redraw(),该方法再次调用创建内容方法,并且应该提供您正在寻找的结果。

我希望这有帮助。

You have to refresh the label after setting the text. The Control API (which is the superclass for all SWT components) has a method redraw(), which, calls the create contents method again and should provide the result you are looking for.

I hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文