How to update SWT GUI from another thread in Java

前端 未结 4 723
野性不改
野性不改 2020-12-05 11:36

I am writing a desktop application using SWT. What is the simplest way to update GUI controls from another thread?

4条回答
  •  暖寄归人
    2020-12-05 12:17

    Use Display.asyncExec or Display.syncExec, depending on your needs.

    For example, another thread might call this method to safely update a label:

      private static void doUpdate(final Display display, final Label target,
          final String value) {
        display.asyncExec(new Runnable() {
          @Override
          public void run() {
            if (!target.isDisposed()) {
              target.setText(value);
              target.getParent().layout();
            }
          }
        });
      }
    
    • More here

提交回复
热议问题