站内搜索: 请输入搜索关键词
当前页面: 图书首页 > SWT: The Standard Widget Toolkit

15.1 When Are Layouts Invoked? - SWT: The Standard Widget Toolkit

Previous Section  < Day Day Up >  Next Section

15.1 When Are Layouts Invoked?

There are two situations in SWT when layout functionality is invoked. Both are the result of some action on a composite.

  • The composite is resized (either programmatically or by the user)

  • The layout() method of class Composite is called

When the composite is resized, the layout places the children of the composite at their new positions, using its layout algorithm. When a child control is resized, it may also contain a layout, causing the process to continue downward in the control hierarchy. The layout is invoked after the SWT.Resize event. This means that you can configure the layout just before it is invoked. Generally speaking, this is something that you will not need to do but there are cases when this feature can be useful (see Forcing Controls to Wrap).

Programmers new to SWT sometimes find it confusing that layout does not happen in more situations. For example, they expect layout to be invoked when a control is hidden, shown, or disposed of, or when a string is assigned into a label.[2] The problem with this approach is that only the programmer really knows when layout should occur. Because layout operations are expensive, layout should be invoked only when necessary. For example, if layout were to happen when each control is disposed of, multiple layouts and redraws would occur in the composite, causing a potential performance problem.[3] The section Forcing a Layout describes how you can make a layout happen.

[2] One implication of this is that hidden controls are included in layout.

[3] One possible way to perform automatic layout would be to implement something similar to the deferred update strategy used for painting. Every method that could change the preferred size of a control would need to queue a layout event. When the system is idle, the layout events would get merged into one. Although this kind of implementation was a possibility, we decided against it for complexity, portability, and performance reasons.

15.1.1 Layout and Z-Order

Most layouts position and resize their controls in the order that they appear in the children list of their composite. This means that when the z-order of a control is changed, using moveAbove() or moveBelow(), the order of the controls in the layout will change.[4] Some programs make use of this feature to move controls within a layout. If you do not change the z-order of a control, the layout order will not change.

[4] FillLayout, RowLayout, and GridLayout are sensitive to z-order changes. FormLayout is not.

    Previous Section  < Day Day Up >  Next Section