Accessing UI from Background Job

In SWT (Eclipse) the UI can only be access and modified from the main Thread. Everything else results in an error:

org.eclipse.swt.SWTException: Invalid thread access

This can be fixed by synchronizing the with the main thread.

In Eclipse e4 a UISynchronize instance can be injected like this:

@Inject private UISynchronize sync;

This allows us to run something from a background job accessing the UI.

sync.asyncExec(new Runnable() {
				
  @Override
  public void run() {
    // your UI access here
  }
});