SWT 强制重新绘制标签
我有一个简单的方法来设置标签的文本:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以循环调用
Display.readAndDispatch()
,直到返回false
。这将确保所有重绘操作都已完成。注意:您可能不想在 UI 线程上调用
controller.loadFile()
。这将导致 UI 挂起。相反,您应该在后台线程中调用controller.loadFile()
。当后台线程完成时,让 UI 线程更新状态。You can call
Display.readAndDispatch()
in a loop until it returnsfalse
. This will ensure all redraw operations have completed.Note: You probably do not want to call
controller.loadFile()
on the UI thread. This will cause the UI to hang. Instead, you should callcontroller.loadFile()
in a background thread. When the background thread finishes, have the UI thread update the status.设置文本后必须刷新标签。 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.