开发者

SWT proper way to populate GUI widget using threads?

开发者 https://www.devze.com 2023-03-31 08:33 出处:网络
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 h

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() {
            开发者_JS百科setStatusToGui(status);
        }
    }
    public class SetStatusRunnable implements Runnable{
        private String status;
        public SetStatusRunnable(String status){
            this.status = status;
        }
        public void run() {
            statusLabel.setText(status);
        }
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号