How to set value on click of link in vaadin

7.4k views Asked by At

I am new to vaadin. I have one Link like

Link link = new Link("", new ExternalResource(redirectURL));

my requirement is, I have to set value when user clicks the link. Can I add listener when user click the link. Or is there alternate ways of setting value if link is clicked.

4

There are 4 answers

1
Abbas On BEST ANSWER

To capture onClick on a link or a label, I always create a HorizontalLayout and put the component inside it:

HorizontalLayout hor = new HorizontalLayout();
final Link link = new Link("Click on Me!", new ExternalResource("http://www.google.com"));
hor.addComponent(link);
hor.addLayoutClickListener(new LayoutClickListener() {
    @Override
    public void layoutClick(LayoutClickEvent event) {
        // capture the click here and do whatever you'd like to do, e.g.
        // if ( event.getClickedComponent() != null ) {
        // if(event.getClickedComponent().equals(link)) {}
    }
});
0
nexus On

You could use the new BrowserWindowOpener class:

From the API:

Component extension that opens a browser popup window when the extended component is clicked.

Example:

BrowserWindowOpener browserWindowOpener = new BrowserWindowOpener(new ExternalResource("http://google.com"));
/*
 * 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
 * 
 * browserWindowOpener.setWindowName();
 */
final Button btn = new Button("Click me");
browserWindowOpener.extend(btn);

btn.addClickListener(new ClickListener() {

    @Override
    public void buttonClick(ClickEvent event) {
        btn.setCaption("clicked");
    }
});

More information here.

0
TheLittleNaruto On

I dint work on Vaadin yet But I looked into the document. I found that the Link class internally extends AbstractComponent class which has many functions which you can override. like it has addListener function where you need to pass the Component listener as a parameter and can detect the click event and do whatever you want to.

For reference check this

and this too

Hope this will help :)

0
acsadam0404 On

I interpreted your question as changing the caption of the link. As far as I know it's not possibly with the Link component. Take a look at the activelink addon: http://vaadin.com/addon/activelink.

This addon behaves like Link and lets you add a LinkActivatedListener to it. The code should look like this:

final ActiveLink link = new ActiveLink("", new ExternalResource(redirectURL));
link.addListener(new LinkActivatedListener() {

    @Override
    public void linkActivated(LinkActivatedEvent event) {
        link.setCaption("newCaption");
    }

});