|
When a Graphic already has a parent, and then you attach it to a new
parent, the Graphic is automatically detached from the
old parent. This sometimes happens when you don't expect it to. For
example, consider this fragment of a Curl applet:
{let h:HBox = {HBox "Click here:"}}
{h.add {CommandButton label="Hello"}}
{value h}
This surprisingly places the button before the "Click here" text.
This is the most common example of the problem. Here's what happened:
At the top level
of a Curl applet, each value is added to the file as it
is computed. The CommandButton is added to the HBox, but the result
of calling add is the thing that was added, so this is immediately
removed from the HBox and added to the document. The {value h}
expression then adds the HBox to the document, but by then all
it contains is the text.
The solution is to surround all of the code in the value block:
{value
{let h:HBox = {HBox "Click here:"}}
{h.add {CommandButton label="Hello"}}
h
}
The {value ...} expression returns only the last expression evaluated.
Thus the result of h.add is ignored and the button is not moved.
|