Vito Vito - 3 months ago 25
Java Question

Updating SWT periodically causes GUI to freeze

I would like to have a SWT-based GUI with text field were values are periodically incremented.

  1. Initially I accessed textField from separate Thread what led to throwing exception:

Exception in thread "Thread-0" org.eclipse.swt.SWTException: Invalid
thread access at org.eclipse.swt.SWT.error( at
org.eclipse.swt.SWT.error( at
org.eclipse.swt.SWT.error( at
org.eclipse.swt.widgets.Widget.error( at
org.eclipse.swt.widgets.Widget.checkWidget( at
org.eclipse.swt.widgets.Text.setText( at
regreon.Incrementing.lambda$0( at

  1. After reading SWT documentation (thanks to @marko-topolnik ) - I tried using
    display.SyncExec(Runnable r)
    display.AsyncExec(Runnable r)
    with runnable that called
    in the loop. But this caused the whole thing to freeze.
    Here is the code:

    package whatever;
    import org.eclipse.swt.widgets.Display;
    import org.eclipse.swt.widgets.Shell;
    import org.eclipse.swt.widgets.Text;
    import org.eclipse.swt.SWT;

    public class FreezingGUI {

    protected Shell shell;
    private Text text;

    public static void main(String[] args) {
    try {
    FreezingGUI window = new FreezingGUI();;
    } catch (Exception e) {

    public void open() {
    Display display = Display.getDefault();

    // HOW TO DO THAT???
    display.syncExec(() -> {
    while (true) {
    try {
    } catch (InterruptedException e) {
    Integer i = Integer.parseInt(text.getText()) + 1;
    while (!shell.isDisposed()) {
    if (!display.readAndDispatch()) {

    protected void createContents() {
    shell = new Shell();
    shell.setSize(450, 300);
    shell.setText("SWT Application");

    text = new Text(shell, SWT.BORDER);
    text.setBounds(30, 32, 78, 26);


How to avoid freezing and throwing exception?


Any SWT operation which changes a UI object must be run on the SWT User Interface thread.

In your case the text.setText(i.toString()); line is an SWT UI operation and is running in a different thread.

You can use the asyncExec or syncExec methods of Display to run some code in the UI thread. So replace:



final String newText = i.toString();
Display.getDefault().asyncExec(() -> text.setText(newText));

(this is assuming you are using Java 8).

Using asyncExec will do the UI update asynchronously. Use syncExec instead if you want to pause the thread until the update is done.

If you are using Java 7 or earlier use:

 final String newText = i.toString();
 Display.getDefault().asyncExec(new Runnable() {
    public void run() {

Note you should also be checking for the Shell being disposed and stopping your background thread. If you don't do this you will get an error when you close the app. Your code incrementing i is also wrong. This thread works:

new Thread(() -> {
    for (int i = 1; true; i++) {
        try {
        } catch (final InterruptedException e) {

        if (shell.isDisposed())  // Stop thread when shell is closed

        final String newText = Integer.toString(i);
        Display.getDefault().asyncExec(() -> text.setText(newText));