SWT 使用线程填充 GUI 小部件的正确方法?

发布于 2024-12-01 08:47:32 字数 2483 浏览 2 评论 0原文

我在尝试找出一种正确填充 SWT GUI 元素的方法时遇到了困难,这需要一些时间(即:我不希望它挂起应用程序)。目前我正在这样做,但我觉得必须有更好的方法。

我必须这样做的原因是因为:

  1. 我需要一个在后台运行的任务来设置一些小部件
  2. 您必须始终从显示的 asyncExec 函数修改 SWT 小部件
  3. 这是我能弄清楚的唯一方法 - 通过创建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:

  1. I needed a task to run in the background that set some widget
  2. You must always modify SWT widgets from the asyncExec function on display
  3. 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 技术交流群。

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

发布评论

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

评论(1

不必在意 2024-12-08 08:47:32

您还可以研究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.

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