View this PageEdit this Page (locked)Attachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide
Hotspots: Admin Pages | Turn-in Site |
Current Links: Cases Final Project Summer 2007

How to change a label dynamically - Chayong Lee

Created By: Chayong Lee

IF YOU DON'T WANT TO READ INTRO AND THE DETAILS OF THE IMPLEMENTATION
SCROLL DOWN TO THE BOTTOM AND YOU WILL SEE THE "SUM UP" PART.

Oftentimes, you will want to change a label on your canvas dynamically.
In my case, it was super useful when I print user information on the canvas at running time.
In order to implement this, you can look up the GUIDevGuide.pdf file as I did.
But I am pretty sure you will end up getting an error message saying "Message Not Understood".

The code given in the GUIDevGuid.pdf file(Page 265) looks like


slogan txt emph label "I delete | | because of the HTML issue"
"Insert the years-in-business into the slogan."
slogan := 'Serving Shrimps For '
, (Date today year - 1869) printString, ' Years'.
( self wrapperAt: #slogan ) labelString: slogan.


The key part is

(self wrapperAt: #slogan) labelString: slogan.

'#slogan' is the ID of the label you want to change and labelString is the method you will use change the label.
So, if you run this code, you should change the label whose ID is #slogan to the string which is being passed into the labelString method.

For example, you create one UI.SimpleDialog class named Label_Test and put one label whose ID is #Label1 on the canvas, and you create one method below.

changeLabel

tempText "I delete | | because of the HTML issue"

texmpText := 'This is just for testing'.
(self wrapperAt: #Label1) labelString: tempText.

After that, you go to the workspace to test this code and type

test:= Label_Test new.
test changeLabel.
test open.

Here, everything looks OK but you will get an error message saying Message Not Understood.
The point is that you are calling changLabel method before you open the canvas.

test changeLabel. gets executed before test open gets executed.

In other words, you haven't created the canvas yet when you called the changeLabel, consequently, the label you want to change is null.

Solution for this situation is quite simple. You need one pre-defined method called

postBuildWith: aBuilder

This is pre-defined method and you are overriding it with your own code which is the changeLabel.
Then, it will automatically get executed after you open the canvas.
The final version of postBuildWith: aBuilder should look like

postBuildWith: aBuilder
self changeLabel.

And you can type on the work space like


test:= Label_Test new.
test open.

Finally, you will see the label which has been changed based on the string you passed into the labelString method.

To sum up,

First, create a class and hook up with the canvas and put one label on the canvas.

Second, create a method which contains

(self wrapperAt: #Label1) labelString: tempText.
make sure that #Label1 is the ID of the label and tempText is a string.

Third, call the method you created above in postBuildWith: aBuilder method.








Link to this Page