SWT 使用线程填充 GUI 小部件的正确方法?
我在尝试找出一种正确填充 SWT GUI 元素的方法时遇到了困难,这需要一些时间(即:我不希望它挂起应用程序)。目前我正在这样做,但我觉得必须有更好的方法。
我必须这样做的原因是因为:
- 我需要一个在后台运行的任务来设置一些小部件
- 您必须始终从显示的 asyncExec 函数修改 SWT 小部件
- 这是我能弄清楚的唯一方法 - 通过创建2 个线程调用函数 - 废话,混乱
请原谅任何代码错误 - 我是从现有项目复制和粘贴的。
public class Gui {
protected Shell shell;
private Display display;
private SomeController someController;
private Label statusLabel;
public void createControllers(){
someController = new someController(this);
}
public void open() {
this.display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
protected void createContents() {
// Create a bunch of stuff
statusLabel = new Label(shell, SWT.NONE);
fd_tabFolder.bottom = new FormAttachment(statusLabel, -6);
Button btnStart = new Button(composite_1, SWT.NONE);
btnStart.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent arg0) {
}
public void widgetSelected(SelectionEvent arg0) {
someController.setStatus("some status");
}
});
}
private Label getStatusLabel(){
return(statusLabel);
}
public Display getDisplay(){
return(display);
}
}
public class SomeController {
private Gui gui;
private Label statusLabel;
public SomeController(Gui gui)
this.gui = gui;
this.statusLabel = gui.getStatusLabel();
}
public void setStatus(String status){
Thread t = new Thread(new SetStatus(status));
t.start();
}
private void setStatusToGui(String status){
gui.getDisplay().asyncExec(new SetStatusRunnable(status));
}
public class SetStatus implements Runnable{
private String status;
public SetStatus(String status){
this.status = status;
}
public void run() {
setStatusToGui(status);
}
}
public class SetStatusRunnable implements Runnable{
private String status;
public SetStatusRunnable(String status){
this.status = status;
}
public void run() {
statusLabel.setText(status);
}
}
}
I am having a rough time trying to figure out a way to properly populate an SWT GUI element that takes some time (ie: I dont want it to hang the APP). Currently I am doing this, but I feel like there has to be a better way.
The reason I had to do it this way was because:
- I needed a task to run in the background that set some widget
- You must always modify SWT widgets from the asyncExec function on display
- This is the only way I could figure it out - with creating 2 threads calling functions - nonsense, messy
Forgive any code errors - I was copying and pasting from an existing project.
public class Gui {
protected Shell shell;
private Display display;
private SomeController someController;
private Label statusLabel;
public void createControllers(){
someController = new someController(this);
}
public void open() {
this.display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
protected void createContents() {
// Create a bunch of stuff
statusLabel = new Label(shell, SWT.NONE);
fd_tabFolder.bottom = new FormAttachment(statusLabel, -6);
Button btnStart = new Button(composite_1, SWT.NONE);
btnStart.addSelectionListener(new SelectionListener() {
public void widgetDefaultSelected(SelectionEvent arg0) {
}
public void widgetSelected(SelectionEvent arg0) {
someController.setStatus("some status");
}
});
}
private Label getStatusLabel(){
return(statusLabel);
}
public Display getDisplay(){
return(display);
}
}
public class SomeController {
private Gui gui;
private Label statusLabel;
public SomeController(Gui gui)
this.gui = gui;
this.statusLabel = gui.getStatusLabel();
}
public void setStatus(String status){
Thread t = new Thread(new SetStatus(status));
t.start();
}
private void setStatusToGui(String status){
gui.getDisplay().asyncExec(new SetStatusRunnable(status));
}
public class SetStatus implements Runnable{
private String status;
public SetStatus(String status){
this.status = status;
}
public void run() {
setStatusToGui(status);
}
}
public class SetStatusRunnable implements Runnable{
private String status;
public SetStatusRunnable(String status){
this.status = status;
}
public void run() {
statusLabel.setText(status);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您还可以研究Jobs API,但它是一个非常标准的在线程上做一些工作,然后调用 asyncExec 来获取数据。
You can also investigate the Jobs API, but it's a pretty standard thing to do some work on a thread and then call
asyncExec
to get back data as well.