2016-05-26

Link opening a new tab with Vaadin

Hi, my rare readers ;)

Not having much time to post recently, but this one will be very short.

Problem


I just wanted to have a link that opens a URL on a new tab in Vaadin.

Silly Try


My first try was:

 getTable().addGeneratedColumn("confirmLink", new ColumnGenerator() {
  private static final long serialVersionUID = 1L;
  @Override
  public Object generateCell(Table sour
   return new Link("Confirm", new ExternalResource("./confirm?id=" + itemId),
    "_blank",
    0, 0, BorderStyle.NONE
    );
  }
 });

The problem here was that the link was open on a new, very large, window. No tabs at all, in any browser.

Correct Way


After reading something about not set dimensions on a few forum posts, I tried:

 getTable().addGeneratedColumn("confirmLink", new ColumnGenerator() {
  private static final long serialVersionUID = 1L;
  @Override
  public Object generateCell(Table source, Object itemId, Object columnId) {
   Link link = new Link("Confirm", new ExternalResource("./confirm?id=" + itemId));
   link.setTargetName("_blank");
   return link;
  }
 });

And it worked!

The trap


I think this long constructor of the Link class is a little bit misleading, as it receives the target parameter, but also force you to pass dimensions, which are primitive types and cannot be null. Passing dimensions force the browser to open a new window to try to honor the dimensions it received.

Vaadin should have a constructor for Link only with label, resource and target parameters. Hope I have some time to report it soon ;)