4

Is there any way to retrieve the TextMorph added to a StandardWindow in Pharo and then change its text contents?

ruhi mehta
  • 103
  • 3

1 Answers1

4

You can retrieve the contents of a SystemWindow or it's subclass, a StandardWindow by sending it the paneMorphs or paneMorphSatisfying: message.

You can set the contents of a TextMorph via - contents: :)

Evaluate this example line by line in a Workspace or Playground and observe the text in the window:

| textMorph text1 text2 window |
textMorph := TextMorph new.
text1 := 'Smalltalk is cool' asText.
text2 := 'Pharo is cool' asText.
textMorph contents: text1.
window := textMorph openInWindow.
window paneMorphs first contents: text2.

Here we have only one paneMorph, the textMorph. In a more complicated layout, you have to choose the right paneMorph first. Or you would keep a reference to your textMorph in the first place, and would not have to retrieve it from the window...

MartinW
  • 4,966
  • 2
  • 24
  • 60
  • Thank you thats pretty helpful. I have like 4 textmorphs in my window. By keeping references to the textmorphs, did you mean save it as instance variables in the class? – ruhi mehta Jul 27 '15 at 02:12
  • Also, is there a way to dynamically update them? Like if I assign the contents: aVariable to a Smalltalk variable and then update the variable, is it possible that my TextMorph content changes with it? – ruhi mehta Jul 27 '15 at 02:18
  • @ruhimehta Yes exactly, keeping a variable to the morph you want to access again later. – MartinW Jul 27 '15 at 09:15
  • 2
    @ruhimehta You should have a look at the Glamour or Spec framework. Also I remember posting a similar question once: http://stackoverflow.com/q/15534305/2012262 Have a look at the answers there. – MartinW Jul 27 '15 at 09:23