Class OpenInNewTab
- All Implemented Interfaces:
Serializable
Button view = new Button("View invoice");
OpenInNewTab.onClick(view, DownloadHandler.fromInputStream(event -> {
byte[] pdf = invoiceService.createPdf();
return new DownloadResponse(new ByteArrayInputStream(pdf), "invoice.pdf",
"application/pdf", pdf.length);
}));
The tab opens in the browser's own click handler, without waiting for a
server round trip, so popup blockers let it through. Opening a tab from a
server-side click listener, for example with
Page.open, happens
only after the round trip and may be blocked.
The new tab is opened without access to the opening page (noopener)
and without a Referer header (noreferrer).
To save content as a file instead of showing it, use
Download.
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionstatic <T extends Component & ClickNotifier<?>>
RegistrationonClick(T component, DownloadHandler handler) Opens the content of a handler in a new tab when the given component is clicked.static <T extends Component & ClickNotifier<?>>
RegistrationOpens a URL in a new tab when the given component is clicked.
-
Method Details
-
onClick
public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, DownloadHandler handler) Opens the content of a handler in a new tab when the given component is clicked. The version that takes a handler is for content produced on the server; the version that takes a URL is for content the browser can already address.The handler runs only when the new tab requests the content, once per click. The browser shows the content when it can, for example a PDF, an image or plain text. The built-in handlers, such as
DownloadHandler.forFile(java.io.File), are switched to serve the content inline. This changes the given handler instance, so it also serves inline wherever else it is used. A custom handler should callDownloadEvent.inlineitself, since content served as an attachment is downloaded instead of shown.The handler is registered for the clicked component and follows its lifecycle: it is only served while the component is attached, visible and enabled. A button that disables itself on click can be disabled before the new tab requests the content; pass
handler.allowDisabled()to serve the content in that case. The handler thatallowDisabled()returns can no longer be switched to inline, so callinline()on a built-in handler first, for exampleDownloadHandler.forFile(file).inline().allowDisabled().- Type Parameters:
T- the component type, must implementClickNotifier- Parameters:
component- the component whose clicks open the new tab, notnullhandler- produces the content when the new tab requests it, notnull- Returns:
- a registration for removing the behavior from the component; after removal the handler is no longer served
-
onClick
public static <T extends Component & ClickNotifier<?>> Registration onClick(T component, String url) Opens a URL in a new tab when the given component is clicked. The version that takes a URL is for content the browser can already address, such as a static file, a route of the application or an external page; the version that takes a handler is for content produced on the server.- Type Parameters:
T- the component type, must implementClickNotifier- Parameters:
component- the component whose clicks open the new tab, notnullurl- the URL to open, notnull- Returns:
- a registration for removing the behavior from the component
- Throws:
IllegalArgumentException- ifurlis ajavascript:URL
-