Package swingtree.style
Class TextConf
java.lang.Object
swingtree.style.TextConf
An immutable configuration type which holds custom
text as well as placement and font properties used for
rendering text onto a Swing component.
This objects is exposed inside the
This objects is exposed inside the
ComponentStyleDelegate.text(Configurator)
as a way to configure custom text properties of a component
as part of the style API exposed by UIForAnySwing.withStyle(Styler).
Here a simple usage example which
demonstrates how to render text onto the top edge of a
JPanel:
UI.panel()
.withStyle(conf -> conf
.prefSize(180, 100)
.background(Color.CYAN)
.text(textConf -> textConf
.content("Hello World!")
.placement(UI.Placement.TOP)
.font( fontConf -> fontConf.color(Color.DARK_GRAY).size(20) )
)
)
In this small example you can see the usage of content(String),
placement(UI.Placement) and font(Configurator).
But there are much more properties available to configure the text rendering
as part of the style API. Here a full list of all available properties with their respective meaning and default values:
- Content
You can set this property through
content(String). This is the actual text content that should be rendered onto the component. For richer text display with multiple styles for different parts of the text, you can also pass a sequence ofStyledStrings to define the text content. EachStyledStringcan have its own font properties which will be resolved using theFontConfof thisTextConfto be derived from.
Seecontent(StyledString...)andcontent(Tuple)for more details on how to configure the content with multiple styled strings.
The default content is an emptyTupleofStyledStrings in which the text configuration is effectively disabled! - Font
The
FontConfobject is its own rich configuration object which holds all font properties like size, style, color, etc. You can configure it throughfont(Configurator).
The default font configuration isFontConf.none(). - Clip Area
The clip area is an enum the area of the component where the text should be
rendered. So the text will only be visible within this area.
You can configure it throughclipTo(UI.ComponentArea).
The default clip area isUI.ComponentArea.INTERIOR. - Placement Boundary
The placement boundary refers to one of many rectangular bounding boxes that capture
the transitional bounding lines between different
UI.ComponentAreas in the box model (margin|border|padding) of a styled component.
You can configure it throughplacementBoundary(UI.ComponentBoundary).
The default placement boundary isUI.ComponentBoundary.INTERIOR_TO_CONTENT, which honors the padding of the component. If you want to ignore the padding and place the text directly after the border of the component, you can set it toUI.ComponentBoundary.BORDER_TO_INTERIOR. - Placement
The placement is an enum which defines where the text should be placed
according to the
placementBoundary(UI.ComponentBoundary). You can configure it throughplacement(UI.Placement).
The default placement isUI.Placement.UNDEFINED. At render time this is first resolved using the horizontal and vertical alignment from theFontConf; only when those alignments are alsoUI.Placement.UNDEFINEDdoes it behave likeUI.Placement.CENTER. - Offset
The offset holds the x and y placement offset of the text.
You can configure it through
offset(Offset)oroffset(int, int).
The default offset isOffset.none()(0, 0). This property is great for making fine adjustments to the text placement.
However, for a more robust alignment of the text, it is recommended to use theplacement(UI.Placement)andplacementBoundary(UI.ComponentBoundary)properties as a first choice. - Wrap Lines
This is a boolean property, and it defines whether the text should be wrapped into multiple lines
if the text content exceeds the width of the available space inside the component.
You can configure it throughwrapLines(boolean).
The default value istrue, which means that the text will wrap into multiple lines if it exceeds the width of the available space inside the component.
If set tofalse, the text will be rendered in a single line and may overflow the component if the text content is too long. - Auto Preferred Height
Is a boolean property which configures whether the preferred height of the styled component should be computed
from the text you render onto it through this
TextConfand then set as the preferred height of the component.
This will effectively turn the preferred height of the component to a function of the component's width and the displayed text.
It will also take full ownership of the preferred height of the component, which means that a preferred height specified elsewhere in the style configuration of the component will be ignored.
You can configure it throughautoPreferredHeight(boolean).
none() to access the null object of the TextConf type.
It is a convenient way to represent a no-op configuration object which will not have any effect
when applied to a component.-
Field Summary
Fields -
Method Summary
Modifier and TypeMethodDescriptionautoPreferredHeight(boolean autoPreferredHeight) Configures whether the preferred height of the styled component should be computed from the text you render onto it through thisTextConfand then set as the preferred height of the component.clipTo(UI.ComponentArea clipArea) Returns a newTextConfobject with the given clip area defined by aUI.ComponentAreaenum.Returns a newTextConfobject with the given text content.content(Collection<StyledString> styledStrings) Configures thisTextConfobject to render the supplied collection ofStyledStrings as the text content onto the component.
EachStyledStringin the sequence can have its own font properties.content(sprouts.Tuple<StyledString> styledStrings) Configures thisTextConfobject to render the suppliedTupleofStyledStrings as the text content onto the component.content(StyledString... styledStrings) Configures thisTextConfobject to render the supplied sequence ofStyledStrings as the text content onto the component.
EachStyledStringin the sequence can have its own font properties.booleanReturns a newTextConfobject with the given font.font(Configurator<FontConf> fontConfFunction) inthashCode()booleanisNone()offset(int x, int y) Returns aTextConfobject updated with an x and y placement offset.placement(UI.Placement placement) Returns an updatedTextConfobject with the given placement, defined by aUI.Placementenum.placementBoundary(UI.ComponentBoundary placementBoundary) Allows you to narrow down the rectangular placement area of the text in the box model of the underlying component using aUI.ComponentBoundaryenum constant.toString()wrapLines(boolean wrapLines) Configures whether the text should be wrapped into multiple lines if the text content exceeds the width of the available space inside the component.
-
Field Details
-
DEFAULT_LAYER
-
-
Method Details
-
content
Returns a newTextConfobject with the given text content.- Parameters:
textString- The text content to be rendered onto the component.- Returns:
- A new
TextConfobject with the given text content.
-
content
Configures thisTextConfobject to render the supplied sequence ofStyledStrings as the text content onto the component.
EachStyledStringin the sequence can have its own font properties. This allows you to render visually rich and expressive text.- Parameters:
styledStrings- A vararg sequence ofStyledStrings which should be rendered as the text content onto the component.- Returns:
- A new
TextConfobject with the given text content. - Throws:
NullPointerException- if the supplied styledStrings is null.
-
content
Configures thisTextConfobject to render the suppliedTupleofStyledStrings as the text content onto the component. EachStyledStringin the sequence can have its own font properties. This allows you to render visually rich and expressive text.- Parameters:
styledStrings- ATupleofStyledStrings which should be rendered as the text content onto the component.- Returns:
- A new
TextConfobject with the given text content. - Throws:
NullPointerException- if the supplied styledStrings is null.
-
content
Configures thisTextConfobject to render the supplied collection ofStyledStrings as the text content onto the component.
EachStyledStringin the sequence can have its own font properties. This allows you to render visually rich and expressive text.- Parameters:
styledStrings- A collection ofStyledStrings which should be rendered as the text content onto the component.- Returns:
- A new
TextConfobject with the given text content. - Throws:
NullPointerException- if the supplied styledStrings is null.
-
font
Returns a newTextConfobject with the given font configuration defined by a configurator function which takes aFontConfobject and returns an updatedFontConfobject with the desired font properties.- Parameters:
fontConfFunction- A function which takes the current font configuration and returns a new font configuration with the desired properties.- Returns:
- A new
TextConfobject with the given font configuration.
-
font
Returns a newTextConfobject with the given font.- Parameters:
font- The font to be used for rendering the text onto the component.- Returns:
- A new
TextConfobject with the given font.
-
clipTo
Returns a newTextConfobject with the given clip area defined by aUI.ComponentAreaenum. Text positioned outside the clip area will not be visible.- Parameters:
clipArea- The clip area where the text should be rendered onto the component.- Returns:
- A new
TextConfobject with the given clip area.
-
placementBoundary
Allows you to narrow down the rectangular placement area of the text in the box model of the underlying component using aUI.ComponentBoundaryenum constant. The component boundaries can be thought of as rectangular bounding boxes that capture the transitional edges between differentUI.ComponentAreas.
This property ensures that the text is placed inside the transitional bounding box.The following placement boundaries are available:
UI.ComponentBoundary.OUTER_TO_EXTERIOR- The outermost boundary of the entire component, including any margin that might be applied.UI.ComponentBoundary.EXTERIOR_TO_BORDER- The boundary located after the margin but before the border. This tightly wraps the entireUI.ComponentArea.BODY.UI.ComponentBoundary.BORDER_TO_INTERIOR- The boundary located after the border but before the padding. It represents the edge of the component's interior.UI.ComponentBoundary.INTERIOR_TO_CONTENT- The boundary located after the padding. It represents the innermost boundary of the component, where the actual content of the component begins, typically the sub-components of the component.
- Parameters:
placementBoundary- The placement boundary of the component.- Returns:
- A new
TextConfobject with the given placement boundary.
-
placement
Returns an updatedTextConfobject with the given placement, defined by aUI.Placementenum. The placement defines where the text should be placed according to theplacementBoundary(UI.ComponentBoundary).The following placements are available:
UI.Placement.TOP- Placed centered at the top edge of the component.UI.Placement.BOTTOM- Placed centered at the bottom edge of the component.UI.Placement.LEFT- At the left center edge of the component.UI.Placement.RIGHT- The right center edge of the component.UI.Placement.CENTER- Placed on the center of the edges defined by theUI.ComponentBoundary.UI.Placement.TOP_LEFT- Placed at the top left corner of the component.UI.Placement.TOP_RIGHT- Placed at the top right corner of the component.UI.Placement.BOTTOM_LEFT- Placed at the bottom left corner of the component.UI.Placement.BOTTOM_RIGHT- Placed at the bottom right corner of the component.
- Parameters:
placement- The placement of the text, defined by aUI.Placementenum.- Returns:
- An updated
TextConfobject with the desired placement.
-
offset
Returns aTextConfobject updated with an x and y placement offset. The offset holds the x and y placement offset of the text. This property is great for making fine adjustments to the text placement. However, for a more robust alignment of the text, it is recommended to use theplacement(UI.Placement)andplacementBoundary(UI.ComponentBoundary)properties as a first choice.- Parameters:
x- The x placement offset of the text.y- The y placement offset of the text.- Returns:
- An updated
TextConfobject with the given offset.
-
wrapLines
Configures whether the text should be wrapped into multiple lines if the text content exceeds the width of the available space inside the component. The default value istrue, which means that the text will wrap into multiple lines if it exceeds the width of the available space inside the component.- Parameters:
wrapLines- A boolean value which defines whether the text should be wrapped into multiple lines.- Returns:
- An updated
TextConfobject with the given wrap lines property.
-
autoPreferredHeight
Configures whether the preferred height of the styled component should be computed from the text you render onto it through thisTextConfand then set as the preferred height of the component.
This will effectively turn the preferred height of the component to a function of the component's width and the displayed text.
It will also take full ownership of the preferred height of the component, which means that a preferred height specified elsewhere in the style configuration of the component will be ignored.- Parameters:
autoPreferredHeight- If true, then the style engine will compute and set a preferred height for the styled component which is based on the text layout produced by this text configuration.- Returns:
- A new text configuration with the desired auto height behavior.
-
simplified
-
isNone
public boolean isNone() -
equals
-
hashCode
public int hashCode() -
toString
-