Java/SWT GUI Access from Thread

Veröffentlicht von

A thread in Java cannot directly access the GUI. With the following snippet, the access is possible:

Call from the thread:

@Override
public void run() {
    html = "<h1>Huhu</h1>";
    dlg.setSomeStuffToGui(html);    
}

Method in the GUI:

public void setSomeStuffToGui(final String html) {
    Display display = getDisplay();

    display.asyncExec(new Runnable() {
        public void run() {
            //your stuff goes here:
            textField.setText(html);
        }
      });           
}

Eclipse RCP

In an Eclipse RCP application, the snippet looks like that:

final IWorkbench workbench = PlatformUI.getWorkbench();
final Display display = workbench.getDisplay();
 
display.syncExec(new Runnable()
{
   public void run()
   {
      //do stuff
   }
});

Kommentar hinterlassen

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert