How to set value on click of link in vaadin

爷,独闯天下 提交于 2019-12-12 10:17:09

问题


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.


回答1:


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)) {}
    }
});



回答2:


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");
    }

});



回答3:


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.




回答4:


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 :)



来源:https://stackoverflow.com/questions/22679744/how-to-set-value-on-click-of-link-in-vaadin

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