Text

Text provides a primary means for displaying information in the user interface. The WText class provides a simple way to add plain or markup text to the user interface.

The WText widget displays text using an HTML <span> or <div> element (depending on whether it is inline or not). It can display either XHTML formatted text or plain text.

The text contents is contained in a WString. This string class provides at the same time support for localization and internationalization:

  • It is a unicode string (internally store as UTF-8) with an API to interact with plain Java strings.
  • It supports localization, when created using WString#tr(). The actual value corresponding to a key is retrieved from a WLocalizedStrings instance, taking into account the current locale. The default implementation of this interface class uses XML files, which are convenient for specifying XHTML snippets.

In its most simple form, the WText widget displays plain text (escaping special characters as needed).

Example This is an example of plain text. Any contained special XHTML characters, such as "<" and ">", are automatically escaped.
source
  void TextPlain() {
    WText text =
        new WText(
            "This is an example of plain text. Any contained special XHTML characters, such as \"<\" and \">\", are automatically escaped.",
            TextFormat.Plain);
  }

Of course, a WText widget may also display XHTML formatted text.

Example This is XHTML markup text. It supports a safe subset of XHTML tags and attributes, which have only decorative functions.
source
  void TextXHTML() {
    WText text =
        new WText(
            "This is <b>XHTML</b> markup text. It supports a safe subset of XHTML tags and attributes, which have only decorative functions.");
  }

XHTML text that is not read from a localized strings interface (which is considered inherently safe), is protected against unwanted side effects from Cross-Site Scripting (XSS) attacks. The text of an XHTML-formatted WText is filtered using an XML parser and all malicious tags are removed (unless this feature is explicitly by-passed by using the XHTMLUnsafeText text format).

Example

This XHTML text contains JavaScript, wich is filtered by the XSS filter.

A warning is printed in the logs.

source
  void TextXSS() {
    WText text =
        new WText(
            "<p>This XHTML text contains JavaScript, wich is filtered by the XSS filter.</p><script>alert(\"XSS Attack!\");</script><p>A warning is printed in the logs.</p>");
  }

If you want to display a text label associated with a form field then a WLabel is more suitable as it can be linked to the input field, relaying focus to it when clicked.

If you want to display an HTML fragment which contains widgets or other bound contents, then a WTemplate widget is probably what you are looking for.

Top

Events

The functionality of WText is very basic. As this widget derives - like many widgets - from WInteractWidget it may respond to mouse events and also keyboard events if it can be given keyboard focus. A few mouse events are demonstrated below.

Example
This text reacts to clicked()This text reacts to doubleClicked()This text reacts to mouseWentOver()This text reacts to mouseWentOut()
source
  void TextEvents() {
    WContainerWidget container = new WContainerWidget();
    WText text1 = new WText("This text reacts to <tt>clicked()</tt>", (WContainerWidget) container);
    text1.setStyleClass("reactive");
    WText text2 =
        new WText("This text reacts to <tt>doubleClicked()</tt>", (WContainerWidget) container);
    text2.setStyleClass("reactive");
    WText text3 =
        new WText("This text reacts to <tt>mouseWentOver()</tt>", (WContainerWidget) container);
    text3.setStyleClass("reactive");
    WText text4 =
        new WText("This text reacts to <tt>mouseWentOut()</tt>", (WContainerWidget) container);
    text4.setStyleClass("reactive");
    final WText out = new WText((WContainerWidget) container);
    text1
        .clicked()
        .addListener(
            this,
            () -> {
              out.setText("<p>Text was clicked.</p>");
            });
    text2
        .doubleClicked()
        .addListener(
            this,
            () -> {
              out.setText("<p>Text was double clicked.</p>");
            });
    text3
        .mouseWentOver()
        .addListener(
            this,
            () -> {
              out.setText("<p>Mouse went over text.</p>");
            });
    text4
        .mouseWentOut()
        .addListener(
            this,
            () -> {
              out.setText("<p>Mouse went out text.</p>");
            });
  }

ToolTip

You may add a tooltip or deferred tooltip to WText

Tooltip example Some text
source
  void TextToolTip() {
    WText text = new WText("Some text", TextFormat.Plain);
    text.setToolTip("ToolTip", TextFormat.XHTML);
  }
Deferred tooltip example Text
source
  class Text extends WText {
    private static Logger logger = LoggerFactory.getLogger(Text.class);

    Text(WContainerWidget parentContainer) {
      super();
      if (parentContainer != null) parentContainer.addWidget(this);
    }

    public Text() {
      this((WContainerWidget) null);
    }

    WString getCalculateToolTip() {
      return new WString("Deferred tooltip");
    }

    WString getToolTip() {
      return this.getCalculateToolTip();
    }
  }

  void TextDeferredToolTip() {
    Text text = new Text();
    text.setText("Text");
    text.setDeferredToolTip(true);
  }

Top