|
|
< Day Day Up > |
|
7.1 Class Label7.1.1 Example
7.1.2 Label Hierarchy
7.1.3 Label Styles
7.1.4 Label Events (none)
Labels are static widgets that draw text, images, or separators. A static control neither takes focus nor participates in tab traversal. Clicking on a static control with the mouse has no effect. Labels are common, and most programs make use of them. For example, to provide descriptive text, a label is placed in front of a "text entry" field in a dialog. Often a label, acting as a separator, appears in a "wizard" as the line between the wizard content and a row of navigation buttons. Although it is possible to use SWT graphics operations to draw text, images, and separators, using Label instead ensures that the appropriate operating system theme and colors are used. In addition, because Label is implemented using the underlying native label control, platform-specific automated testing tools and screen readers (used to facilitate computing for the visually impaired) will deal with them correctly.[1]
The following code fragment creates a label using the SWT.NONE style. This is used to indicate that the label will display either text or an image.
Label label = new Label(parent, SWT.NONE);
label.setText("User Name:");
It is interesting to note that Label does not support events other than those inherited from Control. This is a feature of all static controls in SWT, not just Label. 7.1.5 Text and ImagesLabels allow you to set the text or image that they display. If you do not set either, the label is empty. An empty label displays the background color or draws using the operating system theme.
7.1.6 WrappingWhen a label is created with the SWT.WRAP style, it will wrap strings that are too long to fit within the control. If you embed a linefeed character '\n' in the string, the label will always wrap the string at this location. Because the rules for wrapping strings are operating system- and locale-specific and because labels implement these rules correctly, you should use a label to display text that might wrap, rather than writing the code to wrap and draw it yourself. The following code fragment creates a label and ensures that it will wrap its text. This is achieved by computing the width required to show the string without wrapping, then setting the width of the label to be half of that value.
Label label = new Label(parent, SWT.WRAP);
label.setText("This is a label with text that wraps.");
Point size = label.computeSize(SWT.DEFAULT, SWT.DEFAULT);
label.setSize(size.x / 2, 200);
Sometimes, even though the SWT.WRAP style is set, when used within Layout (see the Layout chapter), the text does not appear to wrap. For an in-depth discussion of this issue, see Forcing Controls to Wrap in the Layout chapter. 7.1.7 AlignmentIf you create a label with one of the SWT.LEFT, SWT.CENTER, or SWT.RIGHT[3] styles, it will horizontally align the text or image. As well as using the style bits, the setAlignment() method can be used to change the label alignment after the control has been created.[4]
The default alignment for Label is SWT.LEFT. The alignment of a label is typically a property that most programs will not change. Instead, the control itself is often aligned with its siblings using setBounds() or Layout. 7.1.8 SeparatorsLabels are capable of behaving like separators. A separator is a "line" that is drawn by a widget.[5] The number and position of the lines as well as their shading and color are operating system-dependent. Sometimes nothing is drawn at all, depending on the operating system theme.
To create a label that draws a separator, use the SWT.SEPARATOR style. Note that once the label has been created with this style, it will not draw text or an image. Attempts to set and get either of these properties are ignored. The orientation of the separator is controlled using style bits SWT .HORIZONTAL and SWT.VERTICAL. The style bits SWT.SHADOW_IN or SWT.SHADOW_OUT are used to specify the appearance of a shadow for the separator on platforms that support this. Because the appearance of the separator is platform-specific and not all platforms support shadows, these style bits will have no effect on some platforms. One common use for separators, as was described earlier, is to create the line that separates content from buttons in a wizard dialog. The following code fragment creates this kind of separator.
Label separator =
new Label(parent, SWT.SEPARATOR | SWT.SHADOW_OUT);
|
|
|
< Day Day Up > |
|