public class CLabel extends Canvas
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
IMPORTANT: This class is not intended to be subclassed.
Modifier and Type | Field and Description |
---|---|
private int |
align
the alignment.
|
private java.lang.String |
appToolTipText |
private Color |
background |
private Image |
backgroundImage |
private int |
bottomMargin |
private static int |
DEFAULT_MARGIN
Left and right margins
|
private static int |
DRAW_FLAGS |
private static java.lang.String |
ELLIPSIS
a string inserted in the middle of text that has been shortened
|
private static int |
GAP
Gap between icon and text
|
private Color[] |
gradientColors |
private int[] |
gradientPercents |
private boolean |
gradientVertical |
private boolean |
ignoreDispose |
private Image |
image
the current icon
|
private int |
leftMargin |
private int |
rightMargin |
private java.lang.String |
text
the current text
|
private int |
topMargin |
Constructor and Description |
---|
CLabel(Composite parent,
int style)
Constructs a new instance of this class given its parent
and a style value describing its behavior and appearance.
|
Modifier and Type | Method and Description |
---|---|
(package private) char |
_findMnemonic(java.lang.String string) |
private static int |
checkStyle(int style)
Check the style bits to ensure that no invalid styles are applied.
|
Point |
computeSize(int wHint,
int hHint,
boolean changed)
Returns the preferred size of the receiver.
|
private void |
drawBevelRect(GC gc,
int x,
int y,
int w,
int h,
Color topleft,
Color bottomright)
Draw a rectangle in the given colors.
|
int |
getAlignment()
Returns the horizontal alignment.
|
int |
getBottomMargin()
Return the CLabel's bottom margin.
|
Image |
getImage()
Return the CLabel's image or
null . |
int |
getLeftMargin()
Return the CLabel's left margin.
|
int |
getRightMargin()
Return the CLabel's right margin.
|
int |
getStyle()
Returns the receiver's style information.
|
java.lang.String |
getText()
Return the Label's text.
|
java.lang.String |
getToolTipText()
Returns the receiver's tool tip text, or null if it has
not been set.
|
int |
getTopMargin()
Return the CLabel's top margin.
|
private Point |
getTotalSize(Image image,
java.lang.String text)
Compute the minimum size.
|
private void |
initAccessible() |
(package private) void |
onDispose(Event event) |
(package private) void |
onMnemonic(TraverseEvent event) |
(package private) void |
onPaint(PaintEvent event) |
private void |
paintBorder(GC gc,
Rectangle r)
Paint the Label's border.
|
void |
setAlignment(int align)
Set the horizontal alignment of the CLabel.
|
void |
setBackground(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.
|
void |
setBackground(Color[] colors,
int[] percents)
Specify a gradient of colours to be drawn in the background of the CLabel.
|
void |
setBackground(Color[] colors,
int[] percents,
boolean vertical)
Specify a gradient of colours to be drawn in the background of the CLabel.
|
void |
setBackground(Image image)
Set the image to be drawn in the background of the label.
|
void |
setBottomMargin(int bottomMargin)
Set the label's bottom margin, in pixels.
|
void |
setFont(Font font)
Sets 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.
|
void |
setImage(Image image)
Set the label's Image.
|
void |
setLeftMargin(int leftMargin)
Set the label's horizontal left margin, in pixels.
|
void |
setMargins(int leftMargin,
int topMargin,
int rightMargin,
int bottomMargin)
Set the label's margins, in pixels.
|
void |
setRightMargin(int rightMargin)
Set the label's right margin, in pixels.
|
void |
setText(java.lang.String text)
Set the label's text.
|
void |
setToolTipText(java.lang.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.
|
void |
setTopMargin(int topMargin)
Set the label's top margin, in pixels.
|
protected java.lang.String |
shortenText(GC gc,
java.lang.String t,
int width)
Shorten the given text
t so that its length doesn't exceed
the given width. |
private java.lang.String[] |
splitString(java.lang.String text) |
(package private) int |
validateOffset(TextLayout layout,
int offset) |
drawBackground, getCaret, getIME, scroll, setCaret, setIME
changed, checkSubclass, drawBackground, getBackgroundMode, getChildren, getLayout, getLayoutDeferred, getTabList, isLayoutDeferred, layout, layout, layout, layout, layout, setBackgroundMode, setFocus, setLayout, setLayoutDeferred, setTabList
computeTrim, getClientArea, getHorizontalBar, getScrollbarsMode, getVerticalBar
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, 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, setBackgroundImage, setBounds, setBounds, setCapture, setCursor, setDragDetect, setEnabled, setForeground, setLayoutData, setLocation, setLocation, setMenu, setOrientation, setParent, setRedraw, setRegion, setSize, setSize, setTouchEnabled, setVisible, toControl, toControl, toDisplay, toDisplay, traverse, traverse, traverse, update
addDisposeListener, addListener, checkWidget, dispose, getData, getData, getDisplay, getListeners, isDisposed, isListening, notifyListeners, removeDisposeListener, removeListener, removeListener, reskin, setData, setData, toString
private static final int GAP
private static final int DEFAULT_MARGIN
private static final java.lang.String ELLIPSIS
private int align
private int leftMargin
private int topMargin
private int rightMargin
private int bottomMargin
private java.lang.String text
private Image image
private java.lang.String appToolTipText
private boolean ignoreDispose
private Image backgroundImage
private Color[] gradientColors
private int[] gradientPercents
private boolean gradientVertical
private Color background
private static int DRAW_FLAGS
public CLabel(Composite parent, int style)
The style value is either one of the style constants defined in
class SWT
which is applicable to instances of this
class, or must be built by bitwise OR'ing together
(that is, using the int
"|" operator) two or more
of those SWT
style constants. The class description
lists the style constants that are applicable to the class.
Style bits are also inherited from superclasses.
parent
- a widget which will be the parent of the new instance (cannot be null)style
- the style of widget to constructjava.lang.IllegalArgumentException
- SWTException
- SWT.LEFT
,
SWT.RIGHT
,
SWT.CENTER
,
SWT.SHADOW_IN
,
SWT.SHADOW_OUT
,
SWT.SHADOW_NONE
,
getStyle()
private static int checkStyle(int style)
public Point computeSize(int wHint, int hHint, boolean changed)
Control
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.DEFAULT
is 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 be false
, so layout
manager caches can be retained.
computeSize
in class Composite
wHint
- the width hint (can be SWT.DEFAULT
)hHint
- the height hint (can be SWT.DEFAULT
)changed
- true
if the control's contents have changed, and false
otherwiseLayout
,
Control.getBorderWidth()
,
Control.getBounds()
,
Control.getSize()
,
Control.pack(boolean)
,
"computeTrim, getClientArea for controls that implement them"private void drawBevelRect(GC gc, int x, int y, int w, int h, Color topleft, Color bottomright)
char _findMnemonic(java.lang.String string)
public int getAlignment()
public int getBottomMargin()
public Image getImage()
null
.public int getLeftMargin()
public int getRightMargin()
private Point getTotalSize(Image image, java.lang.String text)
public int getStyle()
Widget
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_SCROLL
and SWT.V_SCROLL
bits set.
public java.lang.String getText()
public java.lang.String getToolTipText()
Control
getToolTipText
in class Control
public int getTopMargin()
private void initAccessible()
void onDispose(Event event)
void onMnemonic(TraverseEvent event)
void onPaint(PaintEvent event)
public void setAlignment(int align)
align
- the alignment style of LEFT, RIGHT or CENTERSWTException
- public void setBackground(Color color)
Control
Note: This operation is a hint and may be overridden by the platform. For example, on Windows the background of a Button cannot be changed.
setBackground
in class Control
color
- the new color (or null)public void setBackground(Color[] colors, int[] percents)
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});
colors
- an array of Color that specifies the colors to appear in the gradient
in order of appearance from left to right; The value null
clears the background gradient; the value null
can 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.SWTException
- public void setBackground(Color[] colors, int[] percents, boolean vertical)
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);
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 value null
clears the background gradient; the value null
can 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.SWTException
- public void setBackground(Image image)
image
- the image to be drawn in the backgroundSWTException
- public void setBottomMargin(int bottomMargin)
bottomMargin
- the bottom margin of the label, which must be equal to or greater than zeroSWTException
- public void setFont(Font font)
Control
public void setImage(Image image)
null
clears it.image
- the image to be displayed in the label or nullSWTException
- public void setLeftMargin(int leftMargin)
leftMargin
- the left margin of the label, which must be equal to or greater than zeroSWTException
- public void setMargins(int leftMargin, int topMargin, int rightMargin, int bottomMargin)
leftMargin
- the left margin.topMargin
- the top margin.rightMargin
- the right margin.bottomMargin
- the bottom margin.SWTException
- public void setRightMargin(int rightMargin)
rightMargin
- the right margin of the label, which must be equal to or greater than zeroSWTException
- public void setText(java.lang.String text)
null
clears 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.
text
- the text to be displayed in the label or nullSWTException
- public void setToolTipText(java.lang.String string)
Control
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.
setToolTipText
in class Control
string
- the new tool tip text (or null)public void setTopMargin(int topMargin)
topMargin
- the top margin of the label, which must be equal to or greater than zeroSWTException
- protected java.lang.String shortenText(GC gc, java.lang.String t, int width)
t
so 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.gc
- the gc to use for text measurementt
- the text to shortenwidth
- the width to shorten the text to, in pixelsint validateOffset(TextLayout layout, int offset)
private java.lang.String[] splitString(java.lang.String text)