How to bind LabelMorph / TextMorph to a variable so that Morph reflects the changes of the variable?

  • I have an object with a variable containing a string.
  • I have a window containing LabelMorph / TextMorph (or some other Morph that displays text?).

How to associate LabelMorph / TextMorph with a variable so that the label is updated when the line in the variable changes?

  • Classic Smalltalk-80 dependency / change / update mechanism?
  • Fragment of the announcement of Pharo?
  • something other

How can I do it? Which morph should I use?

+4
source share
3 answers

Depends on what you want to achieve. You might want to take a look at the way to do this with Glamor in the current Moose image. In the do-it workspace:

GLMBasicExamples new magritte openOn: GLMMagrittePersonExample sampleData 

This shows how to work with ads while saving. Earlier examples are the best way to start understanding how to work with Glamor (and because of the way the examplebrowser assembly is created, the Magritte example does not update the list when it is nested):

 GLMBasicExamples open 

This has a few other examples that are updated as they change.

+1
source

The easiest way is to use the String modification:

 UpdatingStringMorph on: self selector: #myLabel 

This will send #myLabel (or any other message) to self (or any other object) and display it.

+7
source

This is a solution provided by Benjamin Van Ryseghem on the Pharo mailing list:

For this kind of situation, my solution is to use a ValueHolder. Instead of storing the string directly in the instance variable, it is in a ValueHolder.

I tried this in the workspace:

 |string label| string := 'Wait till i change..' asValueHolder. label := LabelMorph contents: string contents. string whenChangedDo: [:newValue | label contents: newValue ]. label openInWindow. [ 5 seconds asDelay wait. string value: 'I changed :)' ] fork. 
+2
source

All Articles