Class WAnchor

Direct Known Subclasses:

public class WAnchor
extends WContainerWidget
A widget that represents an HTML anchor (to link to other documents).

Use an anchor to link to another web page, document, internal application path or a resource (which specifies application-dependent content that may be generated by your application on demand). The anchor may contain a label text, an image, or any other widget (as it inherits from WContainerWidget). If you link to a document or external url, and do not want the application to terminate when the user follows the anchor, you must use setTarget(). Even for non-HTML documents, this may be important since pending Ajax requests are cancelled if documents are not served within the browser window in certain browsers.

WAnchor is an inline widget.

Note: If you set a text or image using one of the API methods like setText() or setImage() or a constructor, you should not attempt to remove all contents (using WContainerWidget.clear(), or provide a layout (using WContainerWidget#setLayout()), as this will result in undefined behaviour: the text or image are simply inserted as widgets into the container.


The widget corresponds to the HTML <a> tag and does not provide styling. It can be styled using inline or external CSS as appropriate.

  • Constructor Details

  • Method Details

    • setLink

      public void setLink​(WLink link)
      Sets the link.

      The link may hold a URL, a resource, or an internal path.

      When the link points to a resource, the contents of the link may be generated by your application on demand.

      When the link points to an internal path, activating the anchor will change the application's internal path or open a new session with the given path as initial path). This is the easiest way to let the application participate in browser history, and generate URLs that are bookmarkable and search engine friendly.

    • getLink

      public WLink getLink()
      Returns the link.

      See Also:
      setLink(WLink link)
    • setRef

      public void setRef​(java.lang.String url)
      Use setLink() instead.
      Sets the linked URL (deprecated).

    • setRefInternalPath

      public void setRefInternalPath​(java.lang.String path)
      Use setLink() instead.
      Sets a link to an internal path (deprecated).

    • setResource

      public void setResource​(WResource resource)
      Use setLink() instead.
      Sets a destination resource (deprecated).

      The anchor does not assume ownership of the resource.

    • getResource

      public WResource getResource()
      Use getLink() instead.
      Returns the destination resource (deprecated).

      Returns null if no resource has been set.

    • setText

      public void setText​(java.lang.CharSequence text)
      Sets the label text.

      If no text was previously set, a new WText widget is added using WContainerWidget#addWidget().

    • getText

      public WString getText()
      Returns the label text.

      Returns an empty string if no label was set.

      See Also:
      setText(CharSequence text)
    • setWordWrap

      public void setWordWrap​(boolean wordWrap)
      Configures text word wrapping.

      When wordWrap is true, the text set with setText() may be broken up over multiple lines. When wordWrap is false, the text will displayed on a single line, unless the text contains <br /> tags or other block-level tags.

      The default value is true.

      See Also:
    • setTextFormat

      public void setTextFormat​(TextFormat textFormat)
      Configures the text format.

      The default text format is XHTMLText.

      See Also:
      WText.setTextFormat(TextFormat textFormat)
    • getTextFormat

      public TextFormat getTextFormat()
      Returns the text format.

      See Also:
      setTextFormat(TextFormat textFormat)
    • hasWordWrap

      public boolean hasWordWrap()
      Returns whether the widget may break lines.

      See Also:
      setWordWrap(boolean wordWrap)
    • setImage

      public void setImage​(WImage image)
      Sets an image.

      If an image was previously set, it is deleted. The image is added using WContainerWidget#addWidget().

      Ownership of the image is transferred to the anchor.

    • getImage

      public WImage getImage()
      Returns the image.

      Returns null if no image is set.

      See Also:
      setImage(WImage image)
    • setTarget

      public void setTarget​(AnchorTarget target)
      Sets the location where the linked content should be displayed.

      By default, the linked content is displayed in the application (AnchorTarget.TargetSelf). When the destination is an HTML document, the application is replaced with the new document. When the link is to a document that cannot be displayed in the browser, it is offered for download or opened using an external program, depending on browser settings.

      By setting target to AnchorTarget.TargetNewWindow, the destination is displayed in a new browser window or tab.

      See Also:
    • getTarget

      public AnchorTarget getTarget()
      Returns the location where the linked content should be displayed.

      See Also:
      setTarget(AnchorTarget target)
    • isCanReceiveFocus

      public boolean isCanReceiveFocus()
      Description copied from class: WWidget
      Returns whether the widget can receive focus.

      isCanReceiveFocus in class WWebWidget
      See Also:
      WWidget.setCanReceiveFocus(boolean enabled)
    • getTabIndex

      public int getTabIndex()
      Description copied from class: WWidget
      Returns the tab index.

      getTabIndex in class WWebWidget
      See Also:
      WWidget.setTabIndex(int index)
    • isSetFirstFocus

      public boolean isSetFirstFocus()
      Description copied from class: WWidget
      Set focus on the widget's first descendant.

      Set focus on the widget itself, or on a first descendant which can receive focus.

      Returns whether focus could be set.

      isSetFirstFocus in class WWebWidget
    • propagateSetEnabled

      protected void propagateSetEnabled​(boolean enabled)
      Description copied from class: WWidget
      Propagates that a widget was enabled or disabled through children.

      When enabling or disabling a widget, you usually also want to disable contained children. This method is called by setDisabled() to propagate its state to all children.

      You may want to reimplement this method if they wish to render differently when a widget is disabled. The default implementation will propagate the signal to all children.

      propagateSetEnabled in class WInteractWidget
    • enableAjax

      protected void enableAjax()
      Description copied from class: WWidget
      Progresses to an Ajax-enabled widget.

      This method is called when the progressive bootstrap method is used, and support for AJAX has been detected. The default behavior will upgrade the widget's event handling to use AJAX instead of full page reloads, and propagate the call to its children.

      You may want to reimplement this method if you want to make changes to widget when AJAX is enabled. You should always call the base implementation.

      enableAjax in class WWebWidget
      See Also: