20

I have the following piece of code that I wrote using Vaadin. The code opens the page www.google.com when the user clicks the button.

My question is is there any way for me to specify that the page is to be opened in a new tab?

Thanks.

button.addClickListener(new Button.ClickListener() {
    public void buttonClick(ClickEvent event) {
        getUI().getPage().setLocation("http://www.google.com");
    }
});
Andy
  • 17,423
  • 9
  • 52
  • 69
user3702643
  • 1,465
  • 5
  • 21
  • 48
  • Similar Question: [*Generate an HTML page, and open in a new window, from a Vaadin 8 app*](https://stackoverflow.com/q/51691041/642706) – Basil Bourque Aug 05 '18 at 02:52

5 Answers5

28
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).

VH-NZZ
  • 5,248
  • 4
  • 31
  • 47
  • NP, but remember that the `_blank` window name is subjecting the behavior to the browsers. I suppose that's why Vaadin offers the second method signature. I tested o`_blank` on Firefox & Chrome but I can't tell about any other. HTH. – VH-NZZ Jun 04 '14 at 15:11
  • 1
    Did you get it to open in a new tab ? – Don Srinath Jul 18 '14 at 03:35
  • 1
    @Don No , I can't . It just open in new browser window not with newtab. – Cataclysm Aug 21 '14 at 03:25
  • @Cataclysm , yah that's the thing; at last we ended up showing links as basic html links so that user can choose to open in a new tab as usual. – Don Srinath Aug 21 '14 at 08:05
  • I have the same problem. This opens a link in a new page, not in a new tab. – zygimantus May 02 '18 at 14:50
4

Try the following code:

BrowserWindowOpener opener = new BrowserWindowOpener(new ExternalResource(url));
opener.setFeatures("");
opener.extend(button);
sreg
  • 369
  • 2
  • 12
1

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

Martin Vysny
  • 3,088
  • 28
  • 39
1

None of these answers work for me on Vaadin 24, lots of deprecated methods. This currenly works:

UI.getCurrent().getPage().open("https://stackoverflow.com","_blank");
Scott Hather
  • 443
  • 5
  • 15
0

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);
vitfo
  • 9,781
  • 6
  • 29
  • 30