javavaadindom-eventsvaadin7vaadin-touchkit

How to set value on click of link in vaadin


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.


Solution

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