Class TextConf
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 case 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.
Important: Note thatobstacles(Shape...)are only compatible withTOP_LEFT,TOPandTOP_RIGHT! Any other placement turns off obstacle avoidance... - 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).
- Obstacles
A set of
Shapes (in component coordinates) that the text must skip over and flow around so that it is never rendered on top of.
You can configure it throughobstacles(Shape...)orobstacles(Tuple).
The default value is an emptyTuple, meaning no obstacles are applied, however, if a particular component with a text configuration has child components, then the bounding boxes of those child components will automatically be registered as obstacles.
Important — placement compatibility: obstacle avoidance is only active when
placement(UI.Placement)is one ofUI.Placement.TOP_LEFT,UI.Placement.TOP, orUI.Placement.TOP_RIGHT. Any other placement silently disables obstacle avoidance and renders the text as if no obstacles had been registered. The reason for this restriction is architectural:- The algorithm flows text downward, tracking the y-coordinate of each successive line to query which horizontal intervals are free at that level. This maps cleanly only onto a layout that starts from the top and grows downward.
- A bottom-anchored placement would require running the entire algorithm in reverse (growing upward), roughly doubling the implementation complexity.
- A vertically centred placement creates a circular dependency: the height of the text block depends on the obstacles, but where the lines land vertically depends on the height of the text block. Resolving this correctly would require iterative convergence akin to a fluid-dynamics solver — far beyond the scope of a UI layout engine.
- Obstacles From Children Enabled
A boolean property that controls whether child components of the styled component
are automatically registered as text-layout obstacles.
Whentrue(the default), every child contributes an obstacle shape so that the text flows around the children rather than rendering on top of them.
Whenfalse, child components are completely ignored during obstacle collection regardless of theobstaclesFromChildren(UI.ComponentBoundary)setting.
You can configure it throughobstaclesFromChildrenEnabled(boolean). - Obstacles From Children
A
UI.ComponentBoundaryproperty that selects which boundary layer of each child component is used as its obstacle shape when automatic child-obstacle registration is active (i.e.obstaclesFromChildrenEnabled(boolean)istrue).
Think of the boundaries as an onion peeled inward:UI.ComponentBoundary.OUTER_TO_EXTERIOR(the default) uses the full bounding rectangle of the child including any margin, while inner boundaries such asUI.ComponentBoundary.EXTERIOR_TO_BORDERorUI.ComponentBoundary.BORDER_TO_INTERIORshrink the obstacle progressively inward, letting text flow into the child's margin or border areas respectively. Children without a SwingTree style fall back to the full bounding rectangle for any value.
You can configure it throughobstaclesFromChildren(UI.ComponentBoundary).
If you want to disable automatic child-derived obstacles entirely, you can do so by callingobstaclesFromChildrenEnabled(false).
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()Returns an updatedTextConfwith the given shapes registered as "text obstacles" for the text layout engine.Returns an updatedTextConfwith the givenTupleof shapes registered as "text obstacles" for the text layout engine.obstaclesFromChildren(UI.ComponentBoundary boundary) Configures which boundary layer of each child component should be used when automatically deriving text-layout obstacles from the parent component's children.obstaclesFromChildrenEnabled(boolean enabled) Controls whether the style engine should automatically register child components of the parent as text-layout obstacles for this text configuration.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.
Also note that not all placements are compatible with theobstacles(Shape...)avoidance feature. OnlyTOP_LEFT,TOPandTOP_RIGHTallow for text to avoid obstacles...- 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.
-
obstacles
Returns an updatedTextConfwith the given shapes registered as "text obstacles" for the text layout engine. The text will wrap around and skip over each obstacle and can never be rendered on top of it. Obstacle shapes are specified in component coordinates.Curved shapes such as circles or ellipses are supported as well.
Important — placement compatibility: obstacle avoidance is only active whenplacement(UI.Placement)is one ofUI.Placement.TOP_LEFT,UI.Placement.TOP, orUI.Placement.TOP_RIGHT. Any other placement silently disables obstacle avoidance. See theTextConfclass-level documentation for a detailed explanation of why bottom-anchored and vertically-centred placements cannot support obstacle avoidance. -
obstacles
Returns an updatedTextConfwith the givenTupleof shapes registered as "text obstacles" for the text layout engine. The text will wrap around and skip over each obstacle and can never be rendered on top of it. Obstacle shapes are specified in component coordinates.Curved shapes such as circles or ellipses are supported as well.
Important — placement compatibility: obstacle avoidance is only active whenplacement(UI.Placement)is one ofUI.Placement.TOP_LEFT,UI.Placement.TOP, orUI.Placement.TOP_RIGHT. Any other placement silently disables obstacle avoidance. See theTextConfclass-level documentation for a detailed explanation of why bottom-anchored and vertically-centred placements cannot support obstacle avoidance. -
obstaclesFromChildren
Configures which boundary layer of each child component should be used when automatically deriving text-layout obstacles from the parent component's children.When a styled component has child components, the style engine automatically collects a shape for each child and registers it as an obstacle for the text layout, so that the text flows around the children rather than rendering on top of them.
Think of the boundaries as an onion peeled inward — each boundary defines how far inside a child the obstacle extends. The available values are:
UI.ComponentBoundary.OUTER_TO_EXTERIOR— the full bounding rectangle of the child including any margin (the default).UI.ComponentBoundary.EXTERIOR_TO_BORDER— shrinks the obstacle inward to exclude the child's margin, so text may flow into the margin area.UI.ComponentBoundary.BORDER_TO_INTERIOR— shrinks further to exclude both margin and border, so text may flow through margin and border areas.UI.ComponentBoundary.INTERIOR_TO_CONTENT— shrinks to the content area, letting text flow through margin, border, and padding of the child.
- Parameters:
boundary- The component boundary of each child that should be treated as an obstacle.- Returns:
- An updated
TextConfwith the given child-obstacle boundary setting.
-
obstaclesFromChildrenEnabled
Controls whether the style engine should automatically register child components of the parent as text-layout obstacles for this text configuration.When set to
true(the default), the style engine collects the bounding shape of every child component and adds it to the text obstacles, so that the rendered text flows around the children rather than overlapping them. The exact portion of each child that is treated as an obstacle can be refined withobstaclesFromChildren(UI.ComponentBoundary).When set to
false, child components are completely ignored during obstacle collection. This is useful when you deliberately want text and child components to share the same visual area — for example when a child is a transparent overlay or a decorative element that should not interrupt the text flow.- Parameters:
enabled-trueto enable automatic child-obstacle registration (the default),falseto disable it entirely.- Returns:
- An updated
TextConfwith the given child-obstacle-enabled setting.
-
simplified
-
isNone
public boolean isNone() -
equals
-
hashCode
public int hashCode() -
toString
-