Class CLabel
- All Implemented Interfaces:
Drawable
- Direct Known Subclasses:
MessageLine
If there is not enough space a CLabel uses the following strategy to fit the information into the available space:
ignores the indent in left align mode
ignores the image and the gap
shortens the text by replacing the center portion of the label with an ellipsis
shortens the text by removing the center portion of the label
- Styles:
- LEFT, RIGHT, CENTER, SHADOW_IN, SHADOW_OUT, SHADOW_NONE
- Events:
- (NONE)
This class may be subclassed for the purpose of overriding the default string
shortening algorithm that is implemented in method shortenText().
- See Also:
-
Field Summary
Fields inherited from class org.eclipse.swt.widgets.Widget
nativeZoom -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptioncomputeSize(int wHint, int hHint, boolean changed) Returns the preferred size (in points) of the receiver.intReturns the horizontal alignment.intReturn the CLabel's bottom margin.getImage()Return the CLabel's image ornull.intReturn the CLabel's left margin.intReturn the CLabel's right margin.intgetStyle()Returns the receiver's style information.getText()Return the Label's text.Returns the receiver's tool tip text, or null if it has not been set.intReturn the CLabel's top margin.voidsetAlignment(int align) Set the horizontal alignment of the CLabel.voidsetBackground(Color color) Sets the receiver's background color to the color specified by the argument, or to the default system color for the control if the argument is null.voidsetBackground(Color[] colors, int[] percents) Specify a gradient of colours to be drawn in the background of the CLabel.voidsetBackground(Color[] colors, int[] percents, boolean vertical) Specify a gradient of colours to be drawn in the background of the CLabel.voidsetBackground(Image image) Set the image to be drawn in the background of the label.voidsetBottomMargin(int bottomMargin) Set the label's bottom margin, in points.voidSets the font that the receiver will use to paint textual information to the font specified by the argument, or to the default font for that kind of control if the argument is null.voidSet the label's Image.voidsetLeftMargin(int leftMargin) Set the label's horizontal left margin, in points.voidsetMargins(int leftMargin, int topMargin, int rightMargin, int bottomMargin) Set the label's margins, in points.voidsetRightMargin(int rightMargin) Set the label's right margin, in points.voidSet the label's text.voidsetToolTipText(String string) Sets the receiver's tool tip text to the argument, which may be null indicating that the default tool tip for the control will be shown.voidsetTopMargin(int topMargin) Set the label's top margin, in points.protected StringshortenText(GC gc, String t, int width) Shorten the given texttso that its length doesn't exceed the given width.Methods inherited from class org.eclipse.swt.widgets.Canvas
drawBackground, getCaret, getIME, scroll, setCaret, setIMEMethods inherited from class org.eclipse.swt.widgets.Composite
changed, checkSubclass, drawBackground, getBackgroundMode, getChildren, getLayout, getLayoutDeferred, getTabList, isLayoutDeferred, layout, layout, layout, layout, layout, setBackgroundMode, setFocus, setLayout, setLayoutDeferred, setTabList, toStringMethods inherited from class org.eclipse.swt.widgets.Scrollable
computeTrim, getClientArea, getHorizontalBar, getScrollbarsMode, getVerticalBar, setScrollbarsModeMethods inherited from class org.eclipse.swt.widgets.Control
addControlListener, addDragDetectListener, addFocusListener, addGestureListener, addHelpListener, addKeyListener, addMenuDetectListener, addMouseListener, addMouseMoveListener, addMouseTrackListener, addMouseWheelListener, addPaintListener, addTouchListener, addTraverseListener, computeSize, dragDetect, dragDetect, forceFocus, getAccessible, getBackground, getBackgroundImage, getBorderWidth, getBounds, getCursor, getDragDetect, getEnabled, getFont, getForeground, getLayoutData, getLocation, getMenu, getMonitor, getOrientation, getParent, getRegion, getShell, getSize, getTextDirection, getTouchEnabled, getVisible, internal_dispose_GC, internal_new_GC, isEnabled, isFocusControl, isReparentable, isVisible, moveAbove, moveBelow, pack, pack, print, redraw, redraw, removeControlListener, removeDragDetectListener, removeFocusListener, removeGestureListener, removeHelpListener, removeKeyListener, removeMenuDetectListener, removeMouseListener, removeMouseMoveListener, removeMouseTrackListener, removeMouseWheelListener, removePaintListener, removeTouchListener, removeTraverseListener, requestLayout, setBackgroundImage, setBounds, setBounds, setCapture, setCursor, setDragDetect, setEnabled, setForeground, setLayoutData, setLocation, setLocation, setMenu, setOrientation, setParent, setRedraw, setRegion, setSize, setSize, setTextDirection, setTouchEnabled, setVisible, toControl, toControl, toDisplay, toDisplay, traverse, traverse, traverse, updateMethods inherited from class org.eclipse.swt.widgets.Widget
addDisposeListener, addListener, addTypedListener, checkWidget, dispose, getData, getData, getDisplay, getListeners, getTypedListeners, isAutoDirection, isDisposed, isListening, notifyListeners, removeDisposeListener, removeListener, removeListener, removeListener, removeTypedListener, reskin, setData, setDataMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.eclipse.swt.graphics.Drawable
isAutoScalable
-
Constructor Details
-
CLabel
Constructs a new instance of this class given its parent and a style value describing its behavior and appearance.The style value is either one of the style constants defined in class
SWTwhich is applicable to instances of this class, or must be built by bitwise OR'ing together (that is, using theint"|" operator) two or more of thoseSWTstyle constants. The class description lists the style constants that are applicable to the class. Style bits are also inherited from superclasses.- Parameters:
parent- a widget which will be the parent of the new instance (cannot be null)style- the style of widget to construct- Throws:
IllegalArgumentException-- ERROR_NULL_ARGUMENT - if the parent is null
SWTException-- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the parent
- See Also:
-
-
Method Details
-
computeSize
Description copied from class:ControlReturns the preferred size (in points) of the receiver.The preferred size of a control is the size that it would best be displayed at. The width hint and height hint arguments allow the caller to ask a control questions such as "Given a particular width, how high does the control need to be to show all of the contents?" To indicate that the caller does not wish to constrain a particular dimension, the constant
SWT.DEFAULTis passed for the hint.If the changed flag is
true, it indicates that the receiver's contents have changed, therefore any caches that a layout manager containing the control may have been keeping need to be flushed. When the control is resized, the changed flag will befalse, so layout manager caches can be retained.- Overrides:
computeSizein classControl- Parameters:
wHint- the width hint (can beSWT.DEFAULT)hHint- the height hint (can beSWT.DEFAULT)changed-trueif the control's contents have changed, andfalseotherwise- Returns:
- the preferred size of the control.
- See Also:
-
getAlignment
public int getAlignment()Returns the horizontal alignment. The alignment style (LEFT, CENTER or RIGHT) is returned.- Returns:
- SWT.LEFT, SWT.RIGHT or SWT.CENTER
-
getBottomMargin
public int getBottomMargin()Return the CLabel's bottom margin.- Returns:
- the bottom margin of the label
- Since:
- 3.6
-
getImage
Return the CLabel's image ornull.- Returns:
- the image of the label or null
-
getLeftMargin
public int getLeftMargin()Return the CLabel's left margin.- Returns:
- the left margin of the label
- Since:
- 3.6
-
getRightMargin
public int getRightMargin()Return the CLabel's right margin.- Returns:
- the right margin of the label
- Since:
- 3.6
-
getStyle
public int getStyle()Description copied from class:WidgetReturns the receiver's style information.Note that the value which is returned by this method may not match the value which was provided to the constructor when the receiver was created. This can occur when the underlying operating system does not support a particular combination of requested styles. For example, if the platform widget used to implement a particular SWT widget always has scroll bars, the result of calling this method would always have the
SWT.H_SCROLLandSWT.V_SCROLLbits set. -
getText
Return the Label's text.- Returns:
- the text of the label or null
-
getToolTipText
Description copied from class:ControlReturns the receiver's tool tip text, or null if it has not been set.- Overrides:
getToolTipTextin classControl- Returns:
- the receiver's tool tip text
-
getTopMargin
public int getTopMargin()Return the CLabel's top margin.- Returns:
- the top margin of the label
- Since:
- 3.6
-
setAlignment
public void setAlignment(int align) Set the horizontal alignment of the CLabel. Use the values LEFT, CENTER and RIGHT to align image and text within the available space.- Parameters:
align- the alignment style of LEFT, RIGHT or CENTER- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
- ERROR_INVALID_ARGUMENT - if the value of align is not one of SWT.LEFT, SWT.RIGHT or SWT.CENTER
-
setBackground
Description copied from class:ControlSets the receiver's background color to the color specified by the argument, or to the default system color for the control if the argument is null.Note: This operation is a hint and may be overridden by the platform.
Note: The background color can be overridden by setting a background image.
- Overrides:
setBackgroundin classControl- Parameters:
color- the new color (or null)
-
setBackground
Specify a gradient of colours to be drawn in the background of the CLabel.For example, to draw a gradient that varies from dark blue to blue and then to white and stays white for the right half of the label, use the following call to setBackground:
clabel.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE), display.getSystemColor(SWT.COLOR_BLUE), display.getSystemColor(SWT.COLOR_WHITE), display.getSystemColor(SWT.COLOR_WHITE)}, new int[] {25, 50, 100});- Parameters:
colors- an array of Color that specifies the colors to appear in the gradient in order of appearance from left to right; The valuenullclears the background gradient; the valuenullcan be used inside the array of Color to specify the background color.percents- an array of integers between 0 and 100 specifying the percent of the width of the widget at which the color should change; the size of the percents array must be one less than the size of the colors array.- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
- ERROR_INVALID_ARGUMENT - if the values of colors and percents are not consistent
-
setBackground
Specify a gradient of colours to be drawn in the background of the CLabel.For example, to draw a gradient that varies from dark blue to white in the vertical, direction use the following call to setBackground:
clabel.setBackground(new Color[]{display.getSystemColor(SWT.COLOR_DARK_BLUE), display.getSystemColor(SWT.COLOR_WHITE)}, new int[] {100}, true);- Parameters:
colors- an array of Color that specifies the colors to appear in the gradient in order of appearance from left/top to right/bottom; The valuenullclears the background gradient; the valuenullcan be used inside the array of Color to specify the background color.percents- an array of integers between 0 and 100 specifying the percent of the width/height of the widget at which the color should change; the size of the percents array must be one less than the size of the colors array.vertical- indicate the direction of the gradient. True is vertical and false is horizontal.- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
- ERROR_INVALID_ARGUMENT - if the values of colors and percents are not consistent
- Since:
- 3.0
-
setBackground
Set the image to be drawn in the background of the label.- Parameters:
image- the image to be drawn in the background- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
-
setBottomMargin
public void setBottomMargin(int bottomMargin) Set the label's bottom margin, in points.- Parameters:
bottomMargin- the bottom margin of the label, which must be equal to or greater than zero- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
- Since:
- 3.6
-
setFont
Description copied from class:ControlSets the font that the receiver will use to paint textual information to the font specified by the argument, or to the default font for that kind of control if the argument is null. -
setImage
Set the label's Image. The valuenullclears it.- Parameters:
image- the image to be displayed in the label or null- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
-
setLeftMargin
public void setLeftMargin(int leftMargin) Set the label's horizontal left margin, in points.- Parameters:
leftMargin- the left margin of the label, which must be equal to or greater than zero- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
- Since:
- 3.6
-
setMargins
public void setMargins(int leftMargin, int topMargin, int rightMargin, int bottomMargin) Set the label's margins, in points.- Parameters:
leftMargin- the left margin.topMargin- the top margin.rightMargin- the right margin.bottomMargin- the bottom margin.- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
- Since:
- 3.6
-
setRightMargin
public void setRightMargin(int rightMargin) Set the label's right margin, in points.- Parameters:
rightMargin- the right margin of the label, which must be equal to or greater than zero- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
- Since:
- 3.6
-
setText
Set the label's text. The valuenullclears it.Mnemonics are indicated by an '&' that causes the next character to be the mnemonic. When the user presses a key sequence that matches the mnemonic, focus is assigned to the control that follows the label. On most platforms, the mnemonic appears underlined but may be emphasised in a platform specific manner. The mnemonic indicator character '&' can be escaped by doubling it in the string, causing a single '&' to be displayed.
Note: If control characters like '\n', '\t' etc. are used in the string, then the behavior is platform dependent.
- Parameters:
text- the text to be displayed in the label or null- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
-
setToolTipText
Description copied from class:ControlSets the receiver's tool tip text to the argument, which may be null indicating that the default tool tip for the control will be shown. For a control that has a default tool tip, such as the Tree control on Windows, setting the tool tip text to an empty string replaces the default, causing no tool tip text to be shown.The mnemonic indicator (character '&') is not displayed in a tool tip. To display a single '&' in the tool tip, the character '&' can be escaped by doubling it in the string.
NOTE: This operation is a hint and behavior is platform specific, on Windows for CJK-style mnemonics of the form " (&C)" at the end of the tooltip text are not shown in tooltip.
- Overrides:
setToolTipTextin classControl- Parameters:
string- the new tool tip text (or null)
-
setTopMargin
public void setTopMargin(int topMargin) Set the label's top margin, in points.- Parameters:
topMargin- the top margin of the label, which must be equal to or greater than zero- Throws:
SWTException-- ERROR_WIDGET_DISPOSED - if the receiver has been disposed
- ERROR_THREAD_INVALID_ACCESS - if not called from the thread that created the receiver
- Since:
- 3.6
-
shortenText
Shorten the given texttso that its length doesn't exceed the given width. The default implementation replaces characters in the center of the original string with an ellipsis ("..."). Override if you need a different strategy.- Parameters:
gc- the gc to use for text measurementt- the text to shortenwidth- the width to shorten the text to, in points- Returns:
- the shortened text
-