- java.lang.Object
-
- com.googlecode.lanterna.gui2.AbstractComponent<Label>
-
- com.googlecode.lanterna.gui2.Label
-
- All Implemented Interfaces:
Component
,TextGUIElement
- Direct Known Subclasses:
AnimatedLabel
,FileDialog.FileSystemLocationLabel
public class Label extends AbstractComponent<Label>
Label is a simple read-only text display component. It supports customized colors and multi-line text.
-
-
Field Summary
Fields Modifier and Type Field Description private java.util.EnumSet<SGR>
additionalStyles
private TextColor
backgroundColor
private TextColor
foregroundColor
private TerminalSize
labelSize
private java.lang.Integer
labelWidth
private java.lang.String[]
lines
-
Constructor Summary
Constructors Constructor Description Label(java.lang.String text)
Main constructor, creates a new Label displaying a specific text.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description Label
addStyle(SGR sgr)
Adds an additional SGR style to use when drawing the label, in case it wasn't enabled by the themeprotected ComponentRenderer<Label>
createDefaultRenderer()
When you create a custom component, you need to implement this method and return a Renderer which is responsible for taking care of sizing the component, rendering it and choosing where to place the cursor (if Interactable).TextColor
getBackgroundColor()
Returns the background color used when drawing the label, ornull
if the color is read from the current theme.protected TerminalSize
getBounds(java.lang.String[] lines, TerminalSize currentBounds)
Returns the area, in terminal columns and rows, required to fully draw the lines passed in.TextColor
getForegroundColor()
Returns the foreground color used when drawing the label, ornull
if the color is read from the current theme.java.lang.Integer
getLabelWidth()
Returns the limit how wide the label can grow.java.lang.String
getText()
Returns the text this label is displaying.Label
removeStyle(SGR sgr)
Removes an additional SGR style used when drawing the label, previously added byaddStyle(..)
.Label
setBackgroundColor(TextColor backgroundColor)
Overrides the current theme's background color and use the one specified.Label
setForegroundColor(TextColor foregroundColor)
Overrides the current theme's foreground color and use the one specified.Label
setLabelWidth(java.lang.Integer labelWidth)
Use this method to limit how wide the label can grow.protected void
setLines(java.lang.String[] lines)
Protected access to set the internal representation of the text in this label, to be used by sub-classes of label in certain cases wheresetText(..)
doesn't work.void
setText(java.lang.String text)
Updates the text this label is displayingprotected java.lang.String[]
splitIntoMultipleLines(java.lang.String text)
Utility method for taking a string and turning it into an array of lines.-
Methods inherited from class com.googlecode.lanterna.gui2.AbstractComponent
addTo, calculatePreferredSize, draw, getBasePane, getGlobalPosition, getLayoutData, getParent, getPosition, getPreferredSize, getRenderer, getSize, getTextGUI, getTheme, getThemeDefinition, hasParent, invalidate, isInside, isInvalid, isVisible, onAdded, onAfterDrawing, onBeforeDrawing, onRemoved, runOnGUIThreadIfExistsOtherwiseRunDirect, self, setLayoutData, setPosition, setPreferredSize, setRenderer, setSize, setTheme, setVisible, toBasePane, toGlobal, withBorder
-
-
-
-
Field Detail
-
lines
private java.lang.String[] lines
-
labelWidth
private java.lang.Integer labelWidth
-
labelSize
private TerminalSize labelSize
-
foregroundColor
private TextColor foregroundColor
-
backgroundColor
private TextColor backgroundColor
-
additionalStyles
private final java.util.EnumSet<SGR> additionalStyles
-
-
Method Detail
-
setLines
protected void setLines(java.lang.String[] lines)
Protected access to set the internal representation of the text in this label, to be used by sub-classes of label in certain cases wheresetText(..)
doesn't work. In general, you probably want to stick tosetText(..)
instead of this method unless you have a good reason not to.- Parameters:
lines
- New lines this label will display
-
setText
public void setText(java.lang.String text)
Updates the text this label is displaying- Parameters:
text
- New text to display
-
getText
public java.lang.String getText()
Returns the text this label is displaying. Multi-line labels will have their text concatenated with \n, even if they were originally set using multi-line text having \r\n as line terminators.- Returns:
- String of the text this label is displaying
-
splitIntoMultipleLines
protected java.lang.String[] splitIntoMultipleLines(java.lang.String text)
Utility method for taking a string and turning it into an array of lines. This method is used in order to deal with line endings consistently.- Parameters:
text
- Text to split- Returns:
- Array of strings that forms the lines of the original string
-
getBounds
protected TerminalSize getBounds(java.lang.String[] lines, TerminalSize currentBounds)
Returns the area, in terminal columns and rows, required to fully draw the lines passed in.- Parameters:
lines
- Lines to measure the size ofcurrentBounds
- Optional (can passnull
) terminal size to use for storing the output values. If the method is called many times and always returning the same value, passing in an external reference of this size will avoid creating newTerminalSize
objects every time- Returns:
- Size that is required to draw the lines
-
setForegroundColor
public Label setForegroundColor(TextColor foregroundColor)
Overrides the current theme's foreground color and use the one specified. If called withnull
, the override is cleared and the theme is used again.- Parameters:
foregroundColor
- Foreground color to use when drawing the label, ifnull
then use the theme's default- Returns:
- Itself
-
getForegroundColor
public TextColor getForegroundColor()
Returns the foreground color used when drawing the label, ornull
if the color is read from the current theme.- Returns:
- Foreground color used when drawing the label, or
null
if the color is read from the current theme.
-
setBackgroundColor
public Label setBackgroundColor(TextColor backgroundColor)
Overrides the current theme's background color and use the one specified. If called withnull
, the override is cleared and the theme is used again.- Parameters:
backgroundColor
- Background color to use when drawing the label, ifnull
then use the theme's default- Returns:
- Itself
-
getBackgroundColor
public TextColor getBackgroundColor()
Returns the background color used when drawing the label, ornull
if the color is read from the current theme.- Returns:
- Background color used when drawing the label, or
null
if the color is read from the current theme.
-
addStyle
public Label addStyle(SGR sgr)
Adds an additional SGR style to use when drawing the label, in case it wasn't enabled by the theme- Parameters:
sgr
- SGR style to enable for this label- Returns:
- Itself
-
removeStyle
public Label removeStyle(SGR sgr)
Removes an additional SGR style used when drawing the label, previously added byaddStyle(..)
. If the style you are trying to remove is specified by the theme, calling this method will have no effect.- Parameters:
sgr
- SGR style to remove- Returns:
- Itself
-
setLabelWidth
public Label setLabelWidth(java.lang.Integer labelWidth)
Use this method to limit how wide the label can grow. If set tonull
there is no limit but if set to a positive integer then the preferred size will be calculated using word wrapping for lines that are longer than this label width. This may make the label increase in height as new rows may be requested. Please note that some layout managers might assign more space to the label and because of this the wrapping might not be as you expect it. If set to 0, the label will request the same space as if set tonull
, but when drawing it will apply word wrapping instead of truncation in order to fit the label inside the designated area if it's smaller than what was requested. By default this is set to 0.- Parameters:
labelWidth
- Eithernull
or 0 for no limit on how wide the label can be, where 0 indicates word wrapping should be used if the assigned area is smaller than the requested size, or a positive integer setting the requested maximum width at what point word wrapping will begin- Returns:
- Itself
-
getLabelWidth
public java.lang.Integer getLabelWidth()
Returns the limit how wide the label can grow. If set tonull
or 0 there is no limit but if set to a positive integer then the preferred size will be calculated using word wrapping for lines that are longer than the label width. This may make the label increase in height as new rows may be requested. Please note that some layout managers might assign more space to the label and because of this the wrapping might not be as you expect it. If set to 0, the label will request the same space as if set tonull
, but when drawing it will apply word wrapping instead of truncation in order to fit the label inside the designated area if it's smaller than what was requested.- Returns:
- Either
null
or 0 for no limit on how wide the label can be, where 0 indicates word wrapping should be used if the assigned area is smaller than the requested size, or a positive integer setting the requested maximum width at what point word wrapping will begin
-
createDefaultRenderer
protected ComponentRenderer<Label> createDefaultRenderer()
Description copied from class:AbstractComponent
When you create a custom component, you need to implement this method and return a Renderer which is responsible for taking care of sizing the component, rendering it and choosing where to place the cursor (if Interactable). This value is intended to be overridden by custom themes.- Specified by:
createDefaultRenderer
in classAbstractComponent<Label>
- Returns:
- Renderer to use when sizing and drawing this component
-
-