Apparently, the
BrowserWindowOpener
method
setWindowName
uses the HTML5
target
attribute (
no longer deprecated
as it was in HTML4). So you can use either a frame name, or one of four special attribute values:
- _blank
- _self
- _parent
- _top
Using “_blank” should cause a new window (or tab) to open every time it is invoked. Ultimately, the browser decides how it wants to handle this. The example code below works for me in Eclipse Juno (built-in browser), Safari 6, Chrome 24, and Firefox 18 on Mountain Lion Mac.
This
_blank
behavior is documented in the
setWindowName
method of
BrowserWindowOpener
class.
Here is example code that works for me, opening a new window on each invocation…
[color=#7d7c7c]
// Add button whose purpose is: Open new instance of a UI subclass into a new web browser window/tab.
// Create an opener extension
[/color]
BrowserWindowOpener windowOpener = new BrowserWindowOpener(MyUI.class);
windowOpener.setFeatures("height=400,width=600,resizable");
// [color=#07b59e]
'_blank' is a special value for attribute 'target' in HTML5.
[/color]
windowOpener.[color=#22aa00]
[url=https://vaadin.com/api/7.0.0/com/vaadin/server/BrowserWindowOpener.html#setWindowName(java.lang.String)]
setWindowName
[/url]("[color=#99004d]
_blank
[/color]")
[/color];
[color=#7d7c7c]
// Attach it to a button
[/color]
Button openWindowButton = new Button("Open Sesame");
windowOpener.extend(openWindowButton);
[color=#7d7c7c]
// Next, add that button to your layout.
[/color]
Of course, you don’t really want the new window to have a title of “_blank”. So in the init method of your UI subclass, set the window’s desired title.
public class MyUI extends UI {
@Override
protected void init(VaadinRequest request) {
// Set the title of browser window/tab.
[color=#00a21b]
getPage().setTitle("My Title Goes Here");
[/color]
// Generate layout.
MyLayout layout = new MyLayout();
// Place layout in window.
setContent(layout);
}
}
Aside: Some folks use a ‘target’ attribute value of “_new”, believing it has special meaning. That is incorrect. A little Googling taught me that “_new” is
not
a documented special value. The default behavior of using any value other than the four special values (listed above) is to open a new window on first use, and then reuse that same window on successive uses. Whether you name the new window frame “_new” or “pink_elephant”, on successive uses of
target
, the browser will look for a window frame with a matching name and use the found window frame rather than open a new one.
The following is a complete working example app for Vaadin 7.0.0, in three files. Create a new project named “NewWindowExample”.
package com.example.newwindowexample;
import com.vaadin.server.BrowserWindowOpener;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.Button;
import com.vaadin.ui.Button.ClickEvent;
import com.vaadin.ui.Label;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;
/**
* Main UI class
* © 2013 Basil Bourque.
* This source code may be used freely forever by anyone taking full responsibility for doing so.
*/
@SuppressWarnings("serial")
public class NewwindowexampleUI extends UI {
@Override
protected void init(VaadinRequest request) {
final VerticalLayout layout = new VerticalLayout();
layout.setMargin(true);
layout.setSpacing(true);
// Add button whose purpose is: Open new instance of a UI subclass into a new web browser window/tab.
// Create an opener extension
BrowserWindowOpener windowOpener = new BrowserWindowOpener(MyUI.class);
windowOpener.setFeatures("height=100,width=500,resizable");
// '_blank' is a special value for attribute 'target' in HTML5.
windowOpener.setWindowName("_blank");
// Attach it to a button
Button newWindowButton = new Button("Open Sesame");
windowOpener.extend(newWindowButton);
// Next, add that button to your layout.
layout.addComponent(newWindowButton);
// Display.
setContent(layout);
}
}
package com.example.newwindowexample;
import com.vaadin.server.VaadinRequest;
import com.vaadin.ui.UI;
/**
* @author Basil Bourque
* © 2013 Basil Bourque.
* This source code may be used freely forever by anyone taking full responsibility for doing so.
*
*/
public class MyUI extends UI {
@Override
protected void init(VaadinRequest request) {
// Set the title of browser window/tab.
java.util.Date now = new java.util.Date();
getPage().setTitle("Opened at " + now );
// Generate layout.
MyLayout myLayout = new MyLayout(now.toString());
// Place layout in window.
setContent(myLayout);
}
}
package com.example.newwindowexample;
import com.vaadin.ui.Label;
import com.vaadin.ui.VerticalLayout;
/**
* @author Basil Bourque
* © 2013 Basil Bourque.
* This source code may be used freely forever by anyone taking full responsibility for doing so.
*
*/
public class MyLayout extends VerticalLayout {
/**
* Constructor
* @param dateTimeAsString - String with the date-time when opened by user.
*/
public MyLayout(final String dateTimeAsString) {
this.setMargin(true);
this.setSpacing(true);
Label myTitleLabel = new Label(dateTimeAsString + " - Move along. Nothing else to see here.");
this.addComponent(myTitleLabel);
}
}
The Book of Vaadin 7 also has an example, with
source code
.
–Basil Bourque