The Label widget displays a small amount of text. As the name implies, most
labels are used to label another widget such as a Button, a MenuItem, or a
OptionMenu.
Mnemonics
Labels may contain mnemonics. Mnemonics are underlined characters in the
label, used for keyboard navigation. Mnemonics are created by providing a
string with an underscore before the mnemonic character, such as "_File", to
the constructor or setText method, with the hadMnemonic parameter true.
Mnemonics automatically activate any activatable widget the label is inside,
such as a Button; if the label is not inside the mnemonic's target widget,
you have to tell the label about the target using setMnemonicWidget.
Markup (Styled Text)
To make it easy to format text in a label (changing colors, fonts, etc.),
label text can be provided in a simple markup format. Here's how to create a
label with a small font:
label = new Label("");
label.setMarkup("<small>Small text</small>");
(See complete documentation of available tags in the Pango manual.)
The markup passed to setMarkup must be valid; for example, literal
</>/& characters must be escaped as <, >, and
&. If you pass text obtained from the user, file, or a network to
setMarkup, you'll want to escape it with g_markup_escape_text().
Markup strings are just a convenient way to set the PangoAttrList on a label;
setAttributes may be a simpler way to set attributes in some cases. Be
careful though; PangoAttrList tends to cause internationalization problems,
unless you're applying attributes to the entire string . The reason is that
specifying the startIndex and endIndex for a PangoAttribute requires
knowledge of the exact string being displayed, so translations will cause
problems.
Selectable labels
Labels can be made selectable with setSelectable. Selectable labels allow the
user to copy the label contents to the clipboard. Only labels that contain
useful-to-copy information - such as error messages - should be made
selectable.
Text Layout
A label can contain any number of paragraphs, but will have performance
problems if it contains more than a small number. Paragraphs are separated by
newlines or other paragraph separators understood by Pango.
Labels can automatically wrap text if you call setLineWrap.
setJustify sets how the lines in a label align with one another. If you want
to set how the label as a whole aligns in its available space, see
gtk.Misc.setAlignment.
getAngle
public double getAngle()
Gets the angle of rotation for the label.
- the angle of rotation for the label
getAttributes
public AttrList getAttributes()
Gets the attribute list that was set on the label using setAttributes, if
any. This function does not reflect attributes that come from the labels
markup (see setMarkup). If you want to get the effective attributes for
the label, use pango.layout.getAttribute
- the Pango Attributes list
getEllipsize
public EllipsizeMode getEllipsize()
Returns the ellipsizing position.
getJustification
public Justification getJustification()
Returns the justification of the label.
- the justification of the label
getLabel
public String getLabel()
Fetches the text from a label widget including any embedded underlines
indicating mnemonics and Pango markup.
- The entry text of the label.
getLabel
public static Label getLabel(Handle handle)
Construct a label using a handle to a native resource.
getLineWrap
public boolean getLineWrap()
Returns whether lines in the label are automatically wrapped.
- True if lines are automatically wrapped
getMaxWidthChars
public int getMaxWidthChars()
Retrieves the desired maximum width of label, in characters.
- the maximum width of the label in characters.
getMnemonicWidget
public Widget getMnemonicWidget()
Retrieves the target of the mnemonic (keyboard shortcut) of this label
getSelectable
public boolean getSelectable()
Gets the value set by setSelectable.
- TRUE if the user can copy text from the label.
getSelected
public boolean getSelected()
Returns true if any part of the label is selected
getSelectionEnd
public int getSelectionEnd()
Returns the index of the end of the selected test. If no text is
selected, this returns -1.
getSelectionStart
public int getSelectionStart()
Returns the index of the start of the selected text. If the text is not
selected, this returns -1;
getSingleLineMode
public boolean getSingleLineMode()
Returns whether the label is in single line mode.
- true when the label is in single line mode.
getText
public String getText()
Returns the text which is being displayed on this label.
- the text which is being displayed.
getType
public static Type getType()
Retrieve the runtime type used by the GLib library.
- getType in interface Misc
getUseMarkup
public boolean getUseMarkup()
Returns whether the label's text is interpreted as marked up with the
Pango text markup language.
- TRUE if the label's text will be parsed for markup.
getUseMnemonic
public boolean getUseMnemonic()
Returns whether an embedded underline in the label indicates a mnemonic.
- Whether an embedded underline in the label indicates the mnemonic
accelerator keys.
getWidthChars
public int getWidthChars()
Retrieves the desired width of label, in characters.
- the width of the label in characters.
select
public void select(int startOffset,
int endOffset)
Selects a range of characters in the label, if the label is selectable.
If the label is not selectable, this function has no effect. If
startOffset or endOffset are -1, then the end of the label will be
substituted.
startOffset
- The start offset in charactersendOffset
- The end offset in characters
setAngle
public void setAngle(double angle)
Sets the angle of rotation for the label. An angle of 90 reads from from
bottom to top, an angle of 270, from top to bottom. The angle setting for
the label is ignored if the label is selectable, wrapped, or ellipsized.
angle
- the angle that the baseline of the label makes with the
horizontal, in degrees, measured counterclockwise
setAttributes
public void setAttributes(AttrList attributes)
Sets a PangoAttrList; the attributes in the list are applied to the label
text. The attributes set with this function will be ignored if Mnemonics
or markup are being used.
attributes
- Pange attrlist attributes to use
setEllipsize
public void setEllipsize(EllipsizeMode mode)
Sets the mode used to ellipsize (add an ellipsis: "...") the text if
there is not enough space to render the entire string.
setJustification
public void setJustification(Justification justification)
Sets the alignment of the lines in the text of the label relative to each
other. Justification.LEFT is the default value when the widget is first
created. If you instead want to set the alignment of the label as a
whole, use Misc.setAlignment instead. setJustify has no effect on labels
containing only a single line.
setLabel
public void setLabel(String label)
Sets the text of the label.
label
- The new text to set for the label
setLineWrap
public void setLineWrap(boolean wrap)
Toggles line wrapping within the Label widget. TRUE makes it break lines
if text exceeds the widget's size. FALSE lets the text get cut off by the
edge of the widget if it exceeds the widget size.
wrap
- New line wrap setting.
setMarkup
public void setMarkup(String markup)
Parses markup
which is marked up with the Pango text
markup language, setting the label's text and attribute list based on the
parse results. The markup should not contain mnemonic characters if this
method is used.
markup
- String containing pango markup
setMarkup
public void setMarkup(String markup,
boolean hasMnemonic)
Parses markup
which is marked up with the Pango text
markup language, setting the label's text and attribute list based on the
parse results.
markup
- String containing pango markuphasMnemonic
- If true, the markup string will be interpreted to contain
mnemonic characters, for use as keyboard accelerators
setMaxWidthChars
public void setMaxWidthChars(int chars)
Sets the desired maximum width in characters to chars.
chars
- the new desired maximum width, in characters.
setMnemonicWidget
public void setMnemonicWidget(Widget widget)
If the label has been set so that it has an mnemonic key, the label can
be associated with a widget that is the target of the mnemonic. When the
label is inside a widget (like a Button or a Notebook tab) it is
automatically associated with the correct widget, but sometimes (i.e.
when the target is an Entry next to the label) you need to set it
explicitly using this function.
The target widget will be accelerated by emitting "mnemonic_activate" on
it. The default handler for this signal will activate the widget if there
are no mnemonic collisions and toggle focus between the colliding widgets
otherwise.
widget
- The target widget
setSelectable
public void setSelectable(boolean setting)
Selectable labels allow the user to select text from the label, for
copy-and-paste.
setting
- TRUE to allow selecting text in the label
setSingleLineMode
public void setSingleLineMode(boolean singleLineMode)
Sets whether the label is in single line mode.
singleLineMode
- true if the label should be in single line mode
setText
public void setText(String caption)
Changes the text to be displayed in the label widget.
caption
- the new text to be displayed.
setUnderlinePattern
public void setUnderlinePattern(String pattern)
The pattern of underlines you want under the existing text within the
Label widget. For example if the current text of the label says
"FooBarBaz" passing a pattern of "___ ___" will underline "Foo" and "Baz"
but not "Bar".
pattern
- The pattern as described above.
setUseMarkup
public void setUseMarkup(boolean setting)
Sets whether the text of the label contains markup in Pango's text markup
language.
setting
- TRUE if the label's text should be parsed for markup.
setUseMnemonic
public void setUseMnemonic(boolean setting)
If true, an underline in the text indicates the next character should be
used for the mnemonic accelerator key.
setting
- TRUE if underlines in the text indicate mnemonics
setWidthChars
public void setWidthChars(int chars)
Sets the desired width in characters of label to chars.
chars
- the new desired width, in characters.