July 15, 2011

Modifying SWT widgets from outside the event thread / Using a single display with SWT on linux

Like most GUI frameworks , SWT ( which itself is a wrapper over the native windowing system) has an event loop, more over it has other restrictions on the way the widgets/elements within the GUI can be modified. The main item of importance in a SWT based GUI is the display, created like :

Display display = new Display()

now, on linux (x64 at least to the extent I have tested), SWT does not support multiple displays. So you have to use one display entity to drive all your GUI components, this can especially get really troublesome if your GUI has multiple components and windows. Logically making the code modular (splitting the code for each window into may be different classes imposes its own issues) will result in problems.
This is because, SWT allows the GUI widgets to be accessed/modified only from the thread in which the display was created. If you try to access it from outside this thread, you will end up with a SWTException : Invalid thread access.  Here are the possible fixes for this :


  • Declare the display as a static variable so that you have access to it based on the class name
  • Now whenever you need to change something in the GUI, assume you have a function changeWidgetStyle( Params ) which changes the widget style, then you can call the function as : 
                                 <Class Name>.display.asyncExec(new Runnable(){ 
                                                    public void run(){
                                                                  changeWidgetStyle( Params );
                                                    }
                                    });

  • Now the code inside the changeWidgetStyle function can access and modify any components of the GUI. I guess SWT imposes this restriction to make the event thread as responsive as possible.
  • Use the display.syncExec() method with exactly the same syntax if you want to wait for the execution of the function to complete.
I will try to post a code example soon ( as I find some more time). !

No comments:

Post a Comment