Link

Live Demo

The Link component allows making hyperlinks. References to locations are represented as resource objects, explained in “Images and Other Resources”. The Link is a regular HTML hyperlink, that is, an <a href> anchor element that is handled natively by the browser. Unlike when clicking a Button, clicking a Link does not cause an event on the server-side.

Links to an arbitrary URL can be made by using an ExternalResource as follows:

Java

  1. // Textual link
  2. Link link = new Link("Click Me!",
  3. new ExternalResource("http://vaadin.com/"));

See the on-line example.

You can use setIcon() to make image links as follows:

Java

  1. // Image link
  2. Link iconic = new Link(null,
  3. new ExternalResource("http://vaadin.com/"));
  4. iconic.setIcon(new ThemeResource("img/nicubunu_Chain.png"));
  5. // Image + caption
  6. Link combo = new Link("To appease both literal and visual",
  7. new ExternalResource("http://vaadin.com/"));
  8. combo.setIcon(new ThemeResource("img/nicubunu_Chain.png"));

See the on-line example.

The resulting links are shown in Link Example. You could add a “ display: block” style for the icon element to place the caption below it.

link

Link Example

Opening a New Window

With the simple constructor used in the above example, the resource is opened in the current window. Using the constructor that takes the target window as a parameter, or by setting the target window with setTargetName(), you can open the resource in another window, such as a popup browser window/tab. As the target name is an HTML target string managed by the browser, the target can be any window, including windows not managed by the application itself. You can use the special underscored target names, such as _blank to open the link to a new browser window or tab.

Java

  1. // Hyperlink to a given URL
  2. Link link = new Link("Take me a away to a faraway land",
  3. new ExternalResource("http://vaadin.com/"));
  4. // Open the URL in a new window/tab
  5. link.setTargetName("_blank");
  6. // Indicate visually that it opens in a new window/tab
  7. link.setIcon(new ThemeResource("icons/external-link.png"));
  8. link.addStyleName("icon-after-caption");

See the on-line example.

Opening as a Pop-Up Window

With the _blank target, a normal new browser window is opened. If you wish to open it in a popup window (or tab), you need to give a size for the window with setTargetWidth() and setTargetHeight(). You can control the window border style with setTargetBorder(), which takes any of the defined border styles TARGET_BORDER_DEFAULT, TARGET_BORDER_MINIMAL, and TARGET_BORDER_NONE. The exact result depends on the browser.

Java

  1. // Open the URL in a popup
  2. link.setTargetName("_blank");
  3. link.setTargetBorder(Link.TARGET_BORDER_NONE);
  4. link.setTargetHeight(300);
  5. link.setTargetWidth(400);

See the on-line example.

Alternatives

In addition to the Link component, Vaadin allows alternative ways to make hyperlinks. Also, you can make hyperlinks (or any other HTML) in a Label in HTML content mode.

The Button component has a Reindeer.BUTTON_LINK style name that makes it look like a hyperlink, while handling clicks in a server-side click listener instead of in the browser. However, browsers do not generally allow opening new windows from with browser code, so for such tasks you need to use the BrowserWindowOpener extension described in “Opening Pop-up Windows”

CSS Style Rules

CSS

  1. .v-link { }
  2. a { }
  3. .v-icon {}
  4. span {}

The overall style for the Link component is v-link. The root element contains the <a href> hyperlink anchor. Inside the anchor are the icon, with v-icon style, and the caption in a text span.

Hyperlink anchors have a number of pseudo-classes that are active at different times. An unvisited link has a:link class and a visited link a:visited. When the mouse pointer hovers over the link, it will have a:hover, and when the mouse button is being pressed over the link, the a:active class. When combining the pseudo-classes in a selector, please notice that a:hover must come after an a:link and a:visited, and a:active after the a:hover.

Icon Position

Normally, the link icon is before the caption. You can have it right of the caption by reversing the text direction in the containing element.

CSS

  1. /* Position icon right of the link caption. */
  2. .icon-after-caption {
  3. direction: rtl;
  4. }
  5. /* Add some padding around the icon. */
  6. .icon-after-caption .v-icon {
  7. padding: 0 3px;
  8. }

See the on-line example.

The resulting link is shown in Link That Opens a New Window.

link new

Link That Opens a New Window