Vaadin open link in new tab

梦想与她 提交于 2019-11-30 11:33:37
getUI().getPage().open("http://www.google.com", "_blank");

The _blank window name is important here. Beware that you may also have browsers that will might open the resource in a new window instead.

There is also another signature to the open() method, i.e.

open(String url, String windowName, boolean tryToOpenAsPopup) 

that may fit the bill. HTH.

References: Page (Vaadin 7.2.1 API).

Try the following code:

BrowserWindowOpener opener = new BrowserWindowOpener(new ExternalResource(url));
opener.setFeatures("");
opener.extend(button);

It depends on what you want to achieve.

Solution 1

If you want your button to open a new tab, the BrowserWindowOpener might be the right solution.

Button viewBtn = new Button("Click me");
BrowserWindowOpener opener = new BrowserWindowOpener(new ExternalResource("http://www.example.com"));
opener.setWindowName("_blank");
opener.extend(viewBtn);

Solution 2

Your button should open in a new tab only when Ctrl (Alt, Shift, ...) key on the keyboard is hold. If not, open in existing tab. In this case you can try to open a new tab using Page#open() method. Be aware that browsers will probably try to block your action and will warn user that they have blocked a pop-up window (even though it is not a pop-up but a new tab).

Button viewBtn = new Button("Click me", VaadinIcons.EYE);
viewBtn.addClickListener(ev -> {
  if (ev.isCtrlKey()) {
      Page.getCurrent().open("http://www.example.com", "_blank", false);
  } else {
      Page.getCurrent().setLocation("http://www.example.com");
  }
});

Solution 3

If you want the common behavior when the left click opens in existing tab and the middle mouse button click in a new tab, use the link instead of the button. In this case browsers probably let you open a new tab.

Link link = new Link(null, new ExternalResource(UriUtil.createAdUri(ad)));
link.setIcon(VaadinIcons.EYE);

Using Button, BrowserWindowOpener and getUI().getPage().open("http://www.google.com", "_blank"); is discouraged since that is usually blocked by popup blockers.

Instead go with the Link component:

final Link link = new Link("Google", new ExternalResource("http://www.google.com"));
link.setTargetName("_blank");

See more in the Vaadin Link Documentation

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!