[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[news.eclipse.platform.swt] Re: problem updating Text

jda wrote:

> I have a page with the contains two components:
> a Canvas with an Image
> Text (showing year #)

> I have a loop which does some calculations, updates & displays the 
> image, and updates the year.

> As it loops thru, the image changes with each year.
> The problem is that the year does not change with each year.
> It starts out as 0, stays that way until the loop is finished, and then 
> displays 99, despite calling redraw() and update()

> How can I fix this?

> myCanvas extends Canvas, Image image, Text yearNumber

> for (int i = 0; i <= 99; i++)
> {
>    doNumberCrunching();  //calculations will cause image to change

>    myCanvas.resetImageData();   // this changes pixel values
>    image = new Image(display, imageData);

>    myCanvas.redraw(); // put a redraw event on the OS event queue
>    myCanvas.update(); // force redraw

>    // update the year displayed
>    yearNumber.setText(new Integer(i).toString());
>    yearNumber.redraw();
>    yearNumber.update();

> } // end for


My guess is that you are performing this loop in the GUI thread - the
thread the manages the GUI components for the display.  Paint events must
also be processed in this thread.  When you call redraw, you are queing up
a paint event.  However, they can't be executed until control returns from
processing the current event.  Control cannot return until your loop
completes and you return from your method.

What you need to do, is create a thread to perform the loop.  When the
loop needs to update the display, it needs to create a Runnable and pass
it in a call to Display.asyncExec() or Display.syncExec().

You can look at the section on Thread Safety in the following article.

http://www.eclipse.org/articles/Article-Resource-deltas/resource-deltas.html