Squeak
  links to this page:    
View this PageEdit this PageUploads to this PageHistory of this PageTop of the SwikiRecent ChangesSearch the SwikiHelp Guide
Example - aRectangleMorph addMorph: anotherMorph
Last updated at 7:24 am UTC on 25 September 2017
The method
 #addMorph: aMorph
is used to add a submorph (child morph).

 m := RectangleMorph new extent: 320@240; openInWorld.
 m addMorph: (EllipseMorph new color: Color red; position: 50@50).
 m addMorph: (EllipseMorph new color: Color green; position: 100@50).
 m addMorph: (EllipseMorph new color: Color blue; position: 150@50).
 m submorphs   

Here the submorphs get an absolute position.
The result of print-it is

  {an EllipseMorph(1687539) . an EllipseMorph(600081) . an EllipseMorph(2464004)}



If there is no absolute positioning of the submorphs, they get stacked at the origin.
m := RectangleMorph new extent: 320@240; 
         position: 10@30; 
         openInWorld.

m addMorph: (EllipseMorph new color: Color red).
m addMorph: (EllipseMorph new color: Color green).
m addMorph: (EllipseMorph new color: Color blue).


submorphs_2016-08-24.png


Thus a LayoutPolicy is needed.


m := RectangleMorph new extent: 320@240; 
         position: 10@30; 
         layoutPolicy: TableLayout new;
         openInWorld.

m addMorph: (EllipseMorph new color: Color red).
m addMorph: (EllipseMorph new color: Color green).
m addMorph: (EllipseMorph new color: Color blue).


The method #addMorph: adds a submorph in front, thus the blue ellipse which was added last shows up first.

submorphs_with_TableLayout_2016-08-24.png


The method #addMorphBack: adds a morph at the end.

m := RectangleMorph new extent: 320@240; 
         position: 10@30; 
         layoutPolicy: TableLayout new;
         openInWorld.

m addMorphBack: (EllipseMorph new color: Color red).
m addMorphBack: (EllipseMorph new color: Color green).
m addMorphBack: (EllipseMorph new color: Color blue).



submorphs_with_TableLayout_addMorphBack_2016-08-24.png



If more submorphs are added a #wrapDirection: needs to be set for the TableLayout.

m := RectangleMorph new extent: 320@240; 
         position: 10@30; 
         layoutPolicy: TableLayout new;
	 wrapDirection: #leftToRight;
         openInWorld.
10 timesRepeat: [
m addMorphBack: (EllipseMorph new color: Color red).
m addMorphBack: (EllipseMorph new color: Color green).
m addMorphBack: (EllipseMorph new color: Color blue).
]


submorphs_TableLayout_wrapDirection_2016-08-24.png



For more about submorphs consult the System Browser, class Morph categories



See also

How do I get access to a particular submorph?
How to apply some code to all submorphs of a Morph