How to specify a button to open an URL?

11,283

Solution 1

I remember solving a similar problem using a ResourceReference.

Button emailButton = new Button("Email");
content.addComponent(emailButton);
Resource res = new ExternalResource("mailto:[email protected]");
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);
    }
});

Solution 2

For solving similar issue, I applied previously:

    String email="[email protected]";
    Link l=new Link();
    l.setResource(new ExternalResource("mailto:" + email));
    l.setCaption("Send email to " + email);
    addComponent(l);
Share:
11,283
Claas Wilke
Author by

Claas Wilke

Updated on June 05, 2022

Comments

  • Claas Wilke
    Claas Wilke almost 2 years

    I want to write a web application that triggers the default email client of the user to send an email.

    Thus, I created a Link, that leads to an URL conforming to the mailto URI scheme (http://en.wikipedia.org/wiki/Mailto):

    Link emailLink = new Link("Send Email", 
        new ExternalResource("mailto:[email protected]"));
    

    However, instead of using a Link, I want to provide a Button that allows to trigger the respective functionality. But, for buttons I cannot set an ExternalResource to be opened.

    Does anybody know to solve this problem for Buttons, or how to create a Link that looks and behaves exactly like a button? I also tried some CCS modification but did not manage the task by myself. I also found some solutions for former Vaadin versions (https://vaadin.com/forum/#!/thread/69989), but, unfortunately they do not work for Vaadin 7.

  • Jan Galinski
    Jan Galinski about 10 years
    Consider using kris54321' approach, using Resources and Reference is safer than just operating on Strings. Plus: you wont solve the "some browsers block pop ups" problem, thats client config. Consider a world where the ad can decide to ignore popup blockers ... In a company where every user uses the same browser you might succeed via central policies.
  • Claas Wilke
    Claas Wilke about 10 years
    Works like a charm in Google Chrome. However, in my Opera (v. 12.16) nothing happens at all. Any idea why?