Class SJLabel

java.lang.Object
com.vaadin.flow.component.Component
com.vaadin.flow.component.HtmlComponent
com.vaadin.flow.component.HtmlContainer
com.vaadin.flow.component.html.NativeLabel
com.vaadin.swingbridge.surrogates.SJLabel
All Implemented Interfaces:
com.vaadin.flow.component.AttachNotifier, com.vaadin.flow.component.DetachNotifier, com.vaadin.flow.component.HasComponents, com.vaadin.flow.component.HasComponentsOfType<com.vaadin.flow.component.Component>, com.vaadin.flow.component.HasElement, com.vaadin.flow.component.HasEnabled, com.vaadin.flow.component.HasSize, com.vaadin.flow.component.HasStyle, com.vaadin.flow.component.HasText, ComponentMixin, ContainerMixin, JComponentMixin, Serializable

public class SJLabel extends com.vaadin.flow.component.html.NativeLabel implements JComponentMixin
Surrogate for JLabel. Extends Vaadin NativeLabel (<label> element) implements JComponentMixin. Reaches the migrator either through :emulators.JLabel as peer, or as a richer-than-stock Vaadin component in its own right.

Why NativeLabel, not Span

JLabel's labelFor relationship is the <label for="…"> accessibility wiring — Vaadin's NativeLabel.setFor(Component) hits that contract directly, whereas Span doesn't carry the for attribute. Visual shape (one inline element holding text + optional icon) is identical either way, so the labelFor wire is the deciding factor.

DOM management — manual children, not setText

Vaadin HasText.setText(String) writes to the element's text content via Element.setText(String), which removes all children (text nodes and elements) before inserting a single text node. That's incompatible with hosting an icon child element alongside the text. This surrogate sidesteps the issue by:
  • holding an inner Span (textSpan) as a stable text-bearing child — setText writes through the inner Span, leaving any icon sibling untouched;
  • holding the icon (when present) as a Component child inserted at index 0 of the host <label>, so the visual order is icon-then-text matching JDK's horizontalTextPosition == TRAILING default.

R_vaadin_first stance — what's wired, what drops

UI-functional / wired:

  • text — written to textSpan, read back from same.
  • icon — encoded via Icons.toVaadinIconComponent(Object, Icon) (icon-rendering Path 1 / D_icon_rendering); rendered as a Vaadin Image child of the label. Non-ImageIcon Icon impls drop with a WARN (Path 2 / Path 3 deferred).
  • labelFor — delegates to NativeLabel.setFor(Component) so the rendered <label> carries the right for attribute.

Layout — display: inline-grid drives alignment + text-position + gap

The host <label> runs as display: inline-grid, whose tracks are the JDK's icon-plus-text block; the four alignment / text-position setters and iconTextGap all map onto grid CSS:
  • horizontalAlignment → justify-content and verticalAlignment → align-content: where the block sits in the label. Visible only when the label is larger than its content — a shrink-wrap label lays out flush either way, as in the JDK.
  • verticalTextPosition picks the shape: CENTER puts icon and text side by side (grid-auto-flow: column), TOP / BOTTOM stack the text above / below the icon (row).
  • horizontalTextPosition orders the pair side by side (TRAILING / RIGHT icon first, otherwise text first), and when stacked places the text against the icon: CENTER centres it underneath, LEADING hangs it off the icon's leading edge.
  • iconTextGap → CSS gap.

A grid rather than a flexbox because its alignment axes are fixed: a flexbox aligns along its main axis, which turns vertical when the label stacks, so horizontalAlignment would move the block vertically.

R_match_swing_errors IAE preserved before any CSS write (bad SwingConstants axis still throws). Getters parse the CSS back per R_vaadin_first, the shape of setBorder / getBorder (SD_border_css_lossy); CSS has both the physical (left) and the writing-direction-relative (start) value, so every alignment and text position round-trips. The one shape that renders approximately is a CENTER text position side by side, which the JDK paints over the icon: it renders text first.

displayedMnemonic / displayedMnemonicIndex stay Bucket B noops — JLabel's mnemonic visually underlines a character, which has no Vaadin counterpart, and the accelerator-binding aspect needs the labelFor target's focus integration that Vaadin's <label for> doesn't surface client-side. R_match_swing_errors IAE on out-of-range index still fires.

Inherited from JComponentMixin: client properties, borders (CSS round-trip per SD_border_css_lossy), opaque flag, tooltip, key/focus listeners, plus the standard ComponentMixin surface.

See Also:
  • Constructor Details

    • SJLabel

      public SJLabel()
    • SJLabel

      public SJLabel(String text)
    • SJLabel

      public SJLabel(Icon icon)
      JDK convention: an icon-only label centers its content.
    • SJLabel

      public SJLabel(String text, Icon icon)
  • Method Details

    • getText

      public String getText()
      Reads back from the inner textSpan (plain-text mode) or from htmlText when an HTML payload was set. Returns "" (not null) on a freshly-constructed label whose text was never set — Vaadin Span.getText() returns "" for an empty element. Migrated code that null-checks JLabel text needs to ""-check on this layer; the emulator preserves null at its own field shadow.
      Specified by:
      getText in interface com.vaadin.flow.component.HasText
    • setText

      public void setText(String text)
      Writes to the inner textSpan (preserving any icon sibling) and fires the "text" PCE. Mirrors Swing JLabel's two paths: when the string starts with <html> (Swing's BasicHTML.isHTMLString rule, case-insensitive), the inner span receives parsed DOM via innerHTML; outer <html>/<body> wrappers are stripped so the rendered subtree is well-formed. Plain text uses HasText.setText(String). Transitioning HTML → plain clears DOM children before reseeding the text node. null writes land as "".

      Plain text renders on one line (white-space: nowrap), as a JDK JLabel paints it — a \n included, which the JDK does not break on either. HTML text wraps, as the JDK's HTML view does.

      Specified by:
      setText in interface com.vaadin.flow.component.HasText
    • getIcon

      public com.vaadin.flow.component.Component getIcon()
      Returns the Vaadin Component currently installed as the icon child, or null when no icon is set. Vaadin-typed per R_vaadin_first — pure-surrogate users access the icon via this Vaadin handle; migrated code that wants the JDK Icon ref reads it from the emulator layer.
    • setIcon

      public void setIcon(Icon icon)
      Encode the icon via Icons.toVaadinIconComponent(Object, Icon) (Path 1) and install as a child of the host <label>. Replaces any previous icon child; null clears the slot. Fires "icon" PCE with Vaadin-typed values per R_vaadin_first (the JDK Icon ref isn't shadow-stored).
    • getLabelFor

      public com.vaadin.flow.component.Component getLabelFor()
    • setLabelFor

      public void setLabelFor(com.vaadin.flow.component.Component target)
      Routes to NativeLabel.setFor(String) after ensuring the target has a server-side id (Vaadin requires one for the for= attribute write; JDK JLabel.setLabelFor does not). When the target has no id, mint a stable "emul-labelfor-N" suffix and set it before delegating. null clears the slot. Fires "labelFor" PCE.
    • getHorizontalAlignment

      public int getHorizontalAlignment()
    • setHorizontalAlignment

      public void setHorizontalAlignment(int alignment)
    • getVerticalAlignment

      public int getVerticalAlignment()
    • setVerticalAlignment

      public void setVerticalAlignment(int alignment)
    • getHorizontalTextPosition

      public int getHorizontalTextPosition()
    • setHorizontalTextPosition

      public void setHorizontalTextPosition(int textPosition)
    • getVerticalTextPosition

      public int getVerticalTextPosition()
    • setVerticalTextPosition

      public void setVerticalTextPosition(int textPosition)
    • getIconTextGap

      public int getIconTextGap()
    • setIconTextGap

      public void setIconTextGap(int iconTextGap)
    • getDisplayedMnemonic

      public int getDisplayedMnemonic()
    • setDisplayedMnemonic

      public void setDisplayedMnemonic(int key)
    • setDisplayedMnemonic

      public void setDisplayedMnemonic(char aChar)
    • getDisplayedMnemonicIndex

      public int getDisplayedMnemonicIndex()
    • setDisplayedMnemonicIndex

      public void setDisplayedMnemonicIndex(int index)
    • getUIClassID

      public String getUIClassID()
      Description copied from interface: JComponentMixin
      Deliberate no-op — L&F key.
      Specified by:
      getUIClassID in interface JComponentMixin
    • updateUI

      public void updateUI()
      L&F dispatch isn't modeled; matches JComponentMixin's no-op stance.
      Specified by:
      updateUI in interface JComponentMixin