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

4.4 Visibility - SWT: The Standard Widget Toolkit

Previous Section  < Day Day Up >  Next Section

4.4 Visibility

Every control has an associated property called its visibility. A control whose visibility is set to false is said to be hidden, and the act of setting the visibility to false is called hiding the control. A control whose visibility is set to true is said to be visible (not "shown"), and the act of setting the visibility to true is called showing the control.

If a control is visible, SWT will attempt to draw the control, allowing it to be seen by the user. However, it is important to note that being visible is only one of the requirements for a control to be seen. Circumstances that would prevent this include the following.

  • The parent of the control may not be visible. Hiding a parent control hides the children of the control. Showing a parent control shows those children that were not explicitly hidden.

  • The control may be clipped or obscured. If the control is positioned outside of the visible area of its parent (or outside the visible area of the Display) or some other control is drawn in front of the control, it will not be seen.

These circumstances are applied recursively so that if the parent of the control or any of the ancestors cannot be seen for one of the above reasons, the control will also not be seen.

With the exception of Shells, when a control is created, it is visible by default. Shells are invisible when created so that child controls can be created and configured without the user watching this process. The visibility of a control can be changed after creation using setVisible().

setVisible(boolean visible) Shows or hides the control, depending on the boolean parameter. Based on the above rule for parent visibility, if the control is a Shell, dialog shells are shown and hidden as well.

Two methods are provided for checking the visibility state of a control.

getVisible() Returns true if the control is visible and false if it is hidden.

isVisible() Returns true if the control and all ancestors are visible or false if it or any of its ancestors are hidden.

Remember that these methods check only the value of the visibility property, so that even if isVisible() returns true, you may not be able to see the control.

The getVisible() and isVisible() Methods Are Not Equivalent

Despite their use of the JavaBeans naming convention, these methods are not equivalent. Programmers who are new to SWT often make the mistake of using these methods interchangeably.


    Previous Section  < Day Day Up >  Next Section