Reputation: 4418
I want to set text of GWT Label
dynamically.
For example I am using below code to set text in GWT Label
:
Label statusLabel = new Label();
for (int i = 0; i < numX; ++i) {
for (int j = 0; j < numY; ++j) {
statusLabel.setText("Processing " + "(" + (i + 1) + "/" + (j + 1) + " of " + numX + "/" + numY + ") ...");
}
}
And I add this Label
in RootPanel one time like this : RootPanel.get().add(statusLabel);
But problem is that Label
text is unchnage.
What I missed? Or How can set dynamic text into GWT Lable
.
Upvotes: 0
Views: 1135
Reputation: 546
Your loop is just to fast. It does not allow the browser to render the changed text. If your processing is running just inside that loop, the generated JavaScript code will be executed as a single block and will not allow the UI to refresh in-between.
To allow the UI to refresh, you need to use delayed logic.
For example using Scheduler.get().scheduleDeferred
.
Deferred means here the JavaScript code returns control to the browser, which refreshes the text, and afterwards the ScheduledCommand
gets executed.
To make it short:
Split up your processing in different parts, where each of them changes the text and gets scheduled using a ScheduledCommand
.
As an alternative you could keep a counter variable in your class (like in the loop) and just re-schedule ScheduledCommands
until your processing is finished.
Just keep in mind that the browser needs some time to refresh the text and that it won't do that until your JavaScript code returns.
Upvotes: 1