Label: update value from another thread

Hello!
Is it possible to update Label value from another thread?
For example this code runs well:


getLabelRes().setValue("");
String res = executeFunction();
getLabelRes().setValue(res);
getLabelRes().requestRepaint();

but this code fails to update Label:


getLabelRes().setValue("");
new Thread()
	{
	public void run()
		{
			String res = executeFunction();
			getLabelRes().setValue(res);
			getLabelRes().requestRepaint();
		}
	}.start();

I want to do the following: user presses button, some function is running in another thread what makes possible the user to make some actions while function is running. After function ends, label shows the appropriate message.

Can’t say directly what’s wrong with your code, but what you want is possible. The Book gives a complete example in the section on
ProgressIndicator
.

I put the example also
on-line
, so you can see how it works. You can see the (most recent) source code there as well.

There is no need to call requestRepaint() as it is called automatically when the value of the component changes.

The problem you struck into is probably what Marko hinted of - server changes can not be pushed to the client. All changes you do outside the main thread, kicked in action by the user, will not be reflected to the end user until that user (or the users browser) sends a request to the server. When this client request is activated, however, all the changed data in the components will be swooped along in that roundtrip automatically.

As Marko already showed, you can use ProgressIndicator to poll the server every once in a while. Your label changes will tag along with the polling roundtrips. There is a little more lightweight component called the Refresher that does the same thing, but it isn’t part of the core Vaadin framework. Search the forum for it. Pure push functionality is on the drawing board.

2 Marko - thanks for your example
2 Jens Jansson thanks for reply. Adding ProgressIndicator helps to update changes.
One more question

What do you mean by drawing board?

I mean that the Vaadin development team is looking into bulding push functionality into Vaadin, so that we don’t need to poll the server all the time. We’ll get it at some point in the future, if they decide to implement it.