How to specify a button to open an URL?

南楼画角 提交于 2019-12-04 01:40:22

I remember solving a similar problem using a ResourceReference.

Button emailButton = new Button("Email");
content.addComponent(emailButton);
Resource res = new ExternalResource("mailto:someone@example.com");
final ResourceReference rr = ResourceReference.create(res, content, "email");

emailButton.addClickListener(new Button.ClickListener() {

    @Override
    public void buttonClick(ClickEvent event) {
        Page.getCurrent().open(rr.getURL(), null);
    }
});

For solving similar issue, I applied previously:

    String email="info@ORGNAME.org";
    Link l=new Link();
    l.setResource(new ExternalResource("mailto:" + email));
    l.setCaption("Send email to " + email);
    addComponent(l);

After some further tries a managed to adapt the proposed LinkButton solution from https://vaadin.com/forum/#!/thread/69989 for Vaadin 7:

public class LinkButton extends Button {
    public LinkButton(final String url, String caption) {
        super(caption);
        setImmediate(true);
        addClickListener(new Button.ClickListener() {
            private static final long serialVersionUID = -2607584137357484607L;

            @Override
            public void buttonClick(ClickEvent event) {
                LinkButton.this.getUI().getPage().open(url, "_blank");
            }
        });
    }
}

However, this solution is still not perfect as it causes the opening of a popup window being blocked by some web browsers.

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