java.lang.Object
   ↳ com.google.gwt.user.client.ui.UIObject
     ↳ com.google.gwt.user.client.ui.Widget
       ↳ com.google.gwt.user.client.ui.FocusWidget
         ↳ com.google.gwt.user.client.ui.Anchor

Class Overview

A widget that represents a simple <a> element.

If you want use this anchor only for changing history states, use Hyperlink instead.

Built-in Bidi Text Support

This widget is capable of automatically adjusting its direction according to its content. This feature is controlled by setDirectionEstimator(boolean) or passing a DirectionEstimator parameter to the constructor, and is off by default.

CSS Style Rules

  • .gwt-Anchor { }

See Also

Summary

[Expand]
Inherited Constants
From class com.google.gwt.user.client.ui.UIObject
Fields
public static final DirectionEstimator DEFAULT_DIRECTION_ESTIMATOR
[Expand]
Inherited Fields
From interface com.google.gwt.user.client.ui.HasHorizontalAlignment
Public Constructors
Anchor()
Creates an empty anchor.
Anchor(SafeHtml html)
Creates an anchor for scripting.
Anchor(SafeHtml html, HasDirection.Direction dir)
Creates an anchor for scripting.
Anchor(SafeHtml html, DirectionEstimator directionEstimator)
Creates an anchor for scripting.
Anchor(String text)
Creates an anchor for scripting.
Anchor(String text, HasDirection.Direction dir)
Creates an anchor for scripting.
Anchor(String text, DirectionEstimator directionEstimator)
Creates an anchor for scripting.
Anchor(String text, boolean asHtml)
Creates an anchor for scripting.
Anchor(SafeHtml html, String href)
Creates an anchor with its html and href (target URL) specified.
Anchor(SafeHtml html, HasDirection.Direction dir, String href)
Creates an anchor with its html and href (target URL) specified.
Anchor(SafeHtml html, DirectionEstimator directionEstimator, String href)
Creates an anchor with its html and href (target URL) specified.
Anchor(String text, String href)
Creates an anchor with its text and href (target URL) specified.
Anchor(String text, HasDirection.Direction dir, String href)
Creates an anchor with its text and href (target URL) specified.
Anchor(String text, DirectionEstimator directionEstimator, String href)
Creates an anchor with its text and href (target URL) specified.
Anchor(String text, boolean asHTML, String href)
Creates an anchor with its text and href (target URL) specified.
Anchor(SafeHtml html, String href, String target)
Creates a source anchor (link to URI).
Anchor(String text, String href, String target)
Creates a source anchor with a frame target.
Anchor(String text, boolean asHtml, String href, String target)
Creates a source anchor (link to URI).
Protected Constructors
Anchor(Element element)
This constructor may be used by subclasses to explicitly use an existing element.
Public Methods
HasDirection.Direction getDirection()
Gets the directionality of the widget.
DirectionEstimator getDirectionEstimator()
Returns the DirectionEstimator object.
String getHTML()
Gets this object's contents as HTML.
HasHorizontalAlignment.HorizontalAlignmentConstant getHorizontalAlignment()
Gets the horizontal alignment.
String getHref()
Gets the anchor's href (the url to which it links).
String getName()
Gets the widget's name.
int getTabIndex()
Gets the tab index.
String getTarget()
Gets the anchor's target frame (the frame in which navigation will occur when the link is selected).
String getText()
Gets this object's text.
HasDirection.Direction getTextDirection()
Gets the direction of this object's text.
boolean getWordWrap()
Gets whether word-wrapping is enabled.
void setAccessKey(char key)
Sets the widget's 'access key'.
void setDirection(HasDirection.Direction direction)
This method is deprecated. Use setDirectionEstimator(boolean) and / or pass explicit direction to setText(String), setHTML(SafeHtml) instead
void setDirectionEstimator(DirectionEstimator directionEstimator)
Sets the DirectionEstimator object.

Note: DirectionEstimator should be set before the widget has any content; it's highly recommended to set it using a constructor.

void setDirectionEstimator(boolean enabled)
Toggles on / off direction estimation.

See note at setDirectionEstimator(DirectionEstimator).

void setFocus(boolean focused)
Explicitly focus/unfocus this widget.
void setHTML(SafeHtml html)
Sets this object's contents via known-safe HTML.
void setHTML(String html)
Sets this object's contents via HTML.
void setHTML(SafeHtml html, HasDirection.Direction dir)
Sets this object's html, also declaring its direction.
void setHorizontalAlignment(HasHorizontalAlignment.HorizontalAlignmentConstant align)
Sets the horizontal alignment.
void setHref(String href)
Sets the anchor's href (the url to which it links).
void setName(String name)
Sets the widget's name.
void setTabIndex(int index)
Sets the widget's position in the tab index.
void setTarget(String target)
Sets the anchor's target frame (the frame in which navigation will occur when the link is selected).
void setText(String text)
Sets this object's text.
void setText(String text, HasDirection.Direction dir)
Sets this object's text, also declaring its direction.
void setWordWrap(boolean wrap)
Sets whether word-wrapping is enabled.
static Anchor wrap(Element element)
Creates an Anchor widget that wraps an existing <a> element.
[Expand]
Inherited Methods
From class com.google.gwt.user.client.ui.FocusWidget
From class com.google.gwt.user.client.ui.Widget
From class com.google.gwt.user.client.ui.UIObject
From class java.lang.Object
From interface com.google.gwt.event.dom.client.HasBlurHandlers
From interface com.google.gwt.event.dom.client.HasClickHandlers
From interface com.google.gwt.event.dom.client.HasDoubleClickHandlers
From interface com.google.gwt.event.dom.client.HasFocusHandlers
From interface com.google.gwt.event.dom.client.HasGestureChangeHandlers
From interface com.google.gwt.event.dom.client.HasGestureEndHandlers
From interface com.google.gwt.event.dom.client.HasGestureStartHandlers
From interface com.google.gwt.event.dom.client.HasKeyDownHandlers
From interface com.google.gwt.event.dom.client.HasKeyPressHandlers
From interface com.google.gwt.event.dom.client.HasKeyUpHandlers
From interface com.google.gwt.event.dom.client.HasMouseDownHandlers
From interface com.google.gwt.event.dom.client.HasMouseMoveHandlers
From interface com.google.gwt.event.dom.client.HasMouseOutHandlers
From interface com.google.gwt.event.dom.client.HasMouseOverHandlers
From interface com.google.gwt.event.dom.client.HasMouseUpHandlers
From interface com.google.gwt.event.dom.client.HasMouseWheelHandlers
From interface com.google.gwt.event.dom.client.HasTouchCancelHandlers
From interface com.google.gwt.event.dom.client.HasTouchEndHandlers
From interface com.google.gwt.event.dom.client.HasTouchMoveHandlers
From interface com.google.gwt.event.dom.client.HasTouchStartHandlers
From interface com.google.gwt.event.logical.shared.HasAttachHandlers
From interface com.google.gwt.event.shared.HasHandlers
From interface com.google.gwt.i18n.client.HasDirection
From interface com.google.gwt.i18n.shared.HasDirectionEstimator
From interface com.google.gwt.safehtml.client.HasSafeHtml
From interface com.google.gwt.user.client.EventListener
From interface com.google.gwt.user.client.ui.Focusable
From interface com.google.gwt.user.client.ui.HasDirectionalSafeHtml
From interface com.google.gwt.user.client.ui.HasDirectionalText
From interface com.google.gwt.user.client.ui.HasEnabled
From interface com.google.gwt.user.client.ui.HasHTML
From interface com.google.gwt.user.client.ui.HasHorizontalAlignment
From interface com.google.gwt.user.client.ui.HasName
From interface com.google.gwt.user.client.ui.HasText
From interface com.google.gwt.user.client.ui.HasWordWrap
From interface com.google.gwt.user.client.ui.IsWidget
From interface com.google.gwt.user.client.ui.SourcesClickEvents
From interface com.google.gwt.user.client.ui.SourcesFocusEvents
From interface com.google.gwt.user.client.ui.SourcesKeyboardEvents
From interface com.google.gwt.user.client.ui.SourcesMouseEvents

Fields

public static final DirectionEstimator DEFAULT_DIRECTION_ESTIMATOR

Public Constructors

public Anchor ()

Creates an empty anchor.

public Anchor (SafeHtml html)

Creates an anchor for scripting.

Parameters
html the anchor's html

public Anchor (SafeHtml html, HasDirection.Direction dir)

Creates an anchor for scripting. The anchor's href is set to javascript : ;, based on the expectation that listeners will be added to the anchor.

Parameters
html the anchor's html
dir the html's direction

public Anchor (SafeHtml html, DirectionEstimator directionEstimator)

Creates an anchor for scripting. The anchor's href is set to javascript : ;, based on the expectation that listeners will be added to the anchor.

Parameters
html the anchor's html
directionEstimator A DirectionEstimator object used for automatic direction adjustment. For convenience, DEFAULT_DIRECTION_ESTIMATOR can be used.

public Anchor (String text)

Creates an anchor for scripting. The anchor's href is set to javascript:;, based on the expectation that listeners will be added to the anchor.

Parameters
text the anchor's text

public Anchor (String text, HasDirection.Direction dir)

Creates an anchor for scripting. The anchor's href is set to javascript : ;, based on the expectation that listeners will be added to the anchor.

Parameters
text the anchor's text
dir the text's direction

public Anchor (String text, DirectionEstimator directionEstimator)

Creates an anchor for scripting. The anchor's href is set to javascript : ;, based on the expectation that listeners will be added to the anchor.

Parameters
text the anchor's text
directionEstimator A DirectionEstimator object used for automatic direction adjustment. For convenience, DEFAULT_DIRECTION_ESTIMATOR can be used.

public Anchor (String text, boolean asHtml)

Creates an anchor for scripting. The anchor's href is set to javascript:;, based on the expectation that listeners will be added to the anchor.

Parameters
text the anchor's text
asHtml true to treat the specified text as html

public Anchor (SafeHtml html, String href)

Creates an anchor with its html and href (target URL) specified.

Parameters
html the anchor's html
href the url to which it will link

public Anchor (SafeHtml html, HasDirection.Direction dir, String href)

Creates an anchor with its html and href (target URL) specified.

Parameters
html the anchor's html
dir the html's direction
href the url to which it will link

public Anchor (SafeHtml html, DirectionEstimator directionEstimator, String href)

Creates an anchor with its html and href (target URL) specified.

Parameters
html the anchor's html
directionEstimator A DirectionEstimator object used for automatic direction adjustment. For convenience, DEFAULT_DIRECTION_ESTIMATOR can be used.
href the url to which it will link

public Anchor (String text, String href)

Creates an anchor with its text and href (target URL) specified.

Parameters
text the anchor's text
href the url to which it will link

public Anchor (String text, HasDirection.Direction dir, String href)

Creates an anchor with its text and href (target URL) specified.

Parameters
text the anchor's text
dir the text's direction
href the url to which it will link

public Anchor (String text, DirectionEstimator directionEstimator, String href)

Creates an anchor with its text and href (target URL) specified.

Parameters
text the anchor's text
directionEstimator A DirectionEstimator object used for automatic direction adjustment. For convenience, DEFAULT_DIRECTION_ESTIMATOR can be used.
href the url to which it will link

public Anchor (String text, boolean asHTML, String href)

Creates an anchor with its text and href (target URL) specified.

Parameters
text the anchor's text
asHTML true to treat the specified text as html
href the url to which it will link

public Anchor (SafeHtml html, String href, String target)

Creates a source anchor (link to URI). That is, an anchor with an href attribute specifying the destination URI.

Parameters
html the anchor's html
href the url to which it will link
target the target frame (e.g. "_blank" to open the link in a new window)

public Anchor (String text, String href, String target)

Creates a source anchor with a frame target.

Parameters
text the anchor's text
href the url to which it will link
target the target frame (e.g. "_blank" to open the link in a new window)

public Anchor (String text, boolean asHtml, String href, String target)

Creates a source anchor (link to URI). That is, an anchor with an href attribute specifying the destination URI.

Parameters
text the anchor's text
asHtml asHTML true to treat the specified text as html
href the url to which it will link
target the target frame (e.g. "_blank" to open the link in a new window)

Protected Constructors

protected Anchor (Element element)

This constructor may be used by subclasses to explicitly use an existing element. This element must be an <a> element.

Parameters
element the element to be used

Public Methods

public HasDirection.Direction getDirection ()

Gets the directionality of the widget.

Returns
  • RTL if the directionality is right-to-left, LTR if the directionality is left-to-right, or DEFAULT if the directionality is not explicitly specified

public DirectionEstimator getDirectionEstimator ()

Returns the DirectionEstimator object.

public String getHTML ()

Gets this object's contents as HTML.

Returns
  • the object's HTML

public HasHorizontalAlignment.HorizontalAlignmentConstant getHorizontalAlignment ()

Gets the horizontal alignment.

Returns

public String getHref ()

Gets the anchor's href (the url to which it links).

Returns
  • the anchor's href

public String getName ()

Gets the widget's name.

Returns
  • the widget's name

public int getTabIndex ()

Gets the tab index.

Returns
  • the tab index

public String getTarget ()

Gets the anchor's target frame (the frame in which navigation will occur when the link is selected).

Returns
  • the target frame

public String getText ()

Gets this object's text.

Returns
  • the object's text

public HasDirection.Direction getTextDirection ()

Gets the direction of this object's text.

Returns
  • the direction of this object's text

public boolean getWordWrap ()

Gets whether word-wrapping is enabled.

Returns
  • true if word-wrapping is enabled.

public void setAccessKey (char key)

Sets the widget's 'access key'. This key is used (in conjunction with a browser-specific modifier key) to automatically focus the widget.

Parameters
key the widget's access key

public void setDirection (HasDirection.Direction direction)

This method is deprecated.
Use setDirectionEstimator(boolean) and / or pass explicit direction to setText(String), setHTML(SafeHtml) instead

Sets the directionality for a widget.

Parameters
direction RTL if the directionality should be set to right-to-left, LTR if the directionality should be set to left-to-right DEFAULT if the directionality should not be explicitly set

public void setDirectionEstimator (DirectionEstimator directionEstimator)

Sets the DirectionEstimator object.

Note: DirectionEstimator should be set before the widget has any content; it's highly recommended to set it using a constructor. Reason: if the widget already has non-empty content, this will update its direction according to the new estimator's result. This may cause flicker, and thus should be avoided.

Parameters
directionEstimator The {code DirectionEstimator} to be set. null means turning off direction estimation.

public void setDirectionEstimator (boolean enabled)

Toggles on / off direction estimation.

See note at setDirectionEstimator(DirectionEstimator).

Parameters
enabled Whether to enable direction estimation. If true, sets the DirectionEstimator object to a default DirectionEstimator.

public void setFocus (boolean focused)

Explicitly focus/unfocus this widget. Only one widget can have focus at a time, and the widget that does will receive all keyboard events.

Parameters
focused whether this widget should take focus or release it

public void setHTML (SafeHtml html)

Sets this object's contents via known-safe HTML.

The object will behave exactly the same as when a widget's setHTML(String) method is invoked; however the SafeHtml passed to this method observes the contract that it can be used in an HTML context without causing unsafe script execution. Thus, unlike setHTML(String), using this method cannot result in Cross-Site Scripting security vulnerabilities.

Parameters
html the object's new HTML, represented as a SafeHtml object

public void setHTML (String html)

Sets this object's contents via HTML. Use care when setting an object's HTML; it is an easy way to expose script-based security problems. Consider using setText(String) whenever possible.

Parameters
html the object's new HTML

public void setHTML (SafeHtml html, HasDirection.Direction dir)

Sets this object's html, also declaring its direction.

Parameters
html the object's new html
dir the html's direction

public void setHorizontalAlignment (HasHorizontalAlignment.HorizontalAlignmentConstant align)

Sets the horizontal alignment.

Use null to clear horizontal alignment, allowing it to be determined by the standard HTML mechanisms such as inheritance and CSS rules.

Parameters
align the horizontal alignment ( ALIGN_LEFT, ALIGN_CENTER, ALIGN_RIGHT, ALIGN_JUSTIFY, ALIGN_LOCALE_START, or ALIGN_LOCALE_END).

public void setHref (String href)

Sets the anchor's href (the url to which it links).

Parameters
href the anchor's href

public void setName (String name)

Sets the widget's name.

Parameters
name the widget's new name

public void setTabIndex (int index)

Sets the widget's position in the tab index. If more than one widget has the same tab index, each such widget will receive focus in an arbitrary order. Setting the tab index to -1 will cause this widget to be removed from the tab order.

Parameters
index the widget's tab index

public void setTarget (String target)

Sets the anchor's target frame (the frame in which navigation will occur when the link is selected).

Parameters
target the target frame

public void setText (String text)

Sets this object's text.

Parameters
text the object's new text

public void setText (String text, HasDirection.Direction dir)

Sets this object's text, also declaring its direction.

Parameters
text the object's new text
dir the text's direction

public void setWordWrap (boolean wrap)

Sets whether word-wrapping is enabled.

Parameters
wrap true to enable word-wrapping.

public static Anchor wrap (Element element)

Creates an Anchor widget that wraps an existing <a> element. This element must already be attached to the document. If the element is removed from the document, you must call detachNow(Widget).

Parameters
element the element to be wrapped