Events propagation and Handling in Vaadin 8

Hello,
After reading a lot of docs, posts, tutorials I can’t find a solution.
I don’t understand event handling in Vaadin.

The case is :
I try to capture the event(ButtonClick) from a custom component include in another (main) component.
The first CustomComponent (which has to deal with the sent event) is a grid view, the second (the one which should send the event) is just a two buttons CustomComponent (bValid,bCancel).

What I understood (maybe I’m wrong) is the first class should implement listener to capture events.

I use Vaadin 8 and develop Server-Side only.

If someone have an example, a link, or anything that could help me it would be great !!

Hi,

maybe this
Handling Events with Listeners
chapter in the Book of Vaadin will help?

-Olli

Hi,
I already read the doc twice but I can´t find :frowning:
this the reduce code :

public class GroupsConf extends VerticalLayout implements View, Button.ClickListener {

    .........

    @EJB
    private GroupesFacade groupesFacade;

    public GroupsConf() {
        hLayout = new HorizontalLayout();
        vLayoutGroup = new VerticalLayout();
        vLayoutUsers = new VerticalLayout();

        gridGroupes = new Grid<>();
        gridGroupes.setStyleName(STYLE_TITRE_COLONNE_GROUPE);
        gridGroupes.setSizeUndefined();
        vLayoutGroup.addComponent(gridGroupes);

       [b]
 boutons = new BoutonCreerSupprimer();
        vLayoutGroup.addComponent(boutons);
[/b]

        gridUsers = new Grid<>();
        gridUsers.setStyleName(STYLE_TITRE_COLONNE_USERS);
        gridUsers.setSizeUndefined();
        vLayoutUsers.addComponent(gridUsers);
        
    }

    @Override
    public void enter(ViewChangeListener.ViewChangeEvent event) {
        
        
        List<Groupes> listGroupes = groupesFacade.findAll();
        gridGroupes.setItems(listGroupes);
        gridGroupes.addColumn(Groupes::getLibelle).setCaption(TITRE_COLONNE_NOM_GROUPE);
        gridGroupes.addItemClickListener((clicEvent) -> {
            selectGroup(clicEvent);
        });

        gridUsers.addColumn(Utilisateurs::getIdentifiant).setCaption(TITRE_COLONNE_NOM_UTILISATEUR);
        gridUsers.addItemClickListener((clicEvent) -> {
            editUser(clicEvent);
        });

        hLayout.addComponents(vLayoutGroup, vLayoutUsers);

        this.addComponent(hLayout);
        this.setComponentAlignment(hLayout, Alignment.TOP_CENTER);
    }

    private void selectGroup(ItemClick<Groupes> clic) {
        if (clic.getMouseEventDetails().isDoubleClick()) {
            Window editGroupWindow = new Window();
            EditGroups editGroupComponent = new EditGroups();
            editGroupWindow.setContent(editGroupComponent);
            editGroupWindow.center();
            editGroupWindow.setModal(true);
            UI.getCurrent().addWindow(editGroupWindow);
        } else {
            List<Utilisateurs> listUsers = clic.getItem().getUtilisateurs();
            gridUsers.setItems(listUsers);
        }
    }

    private void editUser(ItemClick clicEvent) {
        if (clicEvent.getMouseEventDetails().isDoubleClick()) {
            System.out.println(((Utilisateurs) clicEvent.getItem()).getIdentifiant());
        }
    }

   [b]
 @Override
    public void buttonClick(Button.ClickEvent event) {
        System.out.println("ca clic");
    }
[/b]

The Custom Component :

[code]
public class BoutonCreerSupprimer extends CustomComponent{

public BoutonCreerSupprimer() {
    hLayout = new HorizontalLayout();

    this.delete = new Button(VaadinIcons.TRASH);
    delete.setCaption(CAPTION_DELETE);

    this.create = new Button(VaadinIcons.CHECK);
    create.setCaption(CAPTION_VALID);
    create.addClickListener(this::ok);
    
    
    hLayout.addComponents(create,delete);
    setCompositionRoot(hLayout);
}

private void ok(Button.ClickEvent event){
    System.out.println("echo"); // ok "echo" is printed
    //event.getButton().click(); // SEVERE [com.vaadin.server.DefaultErrorHandler]

(default task-102) : com.vaadin.event.ListenerMethod$MethodException: Invocation of method buttonClick in com.test.BoutonCreerSupprimer$$Lambda$488/1890208777 failed.
//this.fireEvent(event); // nothing printed from buttonClic
}
[/code]What I want is to capture the event (ButtonClick) in the containing component (GroupConf class)
I certainly miss something but what :smiley:

So I changed some things,

now the GroupConf class implement LayoutClickEvent With the fireEvent BoutonCrèerSupprimer class I can catch my event with event.getClickedComponent().getCaption() but it only work if I double click the button

Have you tried passing your object that implements Button.ClickListener to the Button instances instead of using Lambda expressions or method references?

-Olli


Yes I have tried. May be I don’t understand how to manage events, I used to develop UI using AS3 and I thing the events are not propagating/handling the same way, for this time because I can’t find a solution I give up and will create new buttons directly in my main composant.
I read last night the part of the doc “Client-Server integration” , may be a solution for me should be using Connectors and rpc calls.
Read, read, coffee, read, re-coffee, test, test, tea, debug…
I remember why I stopped this profession 4 years ago XD XD XD XD
Thanks for your time.

RPC calls sound like overkill for your situation, it should all be perfectly doable from the server side. I’m not sure what is going wrong exactly, but it does sound like you’re attempting to do something in a peculiar way. One thing to note about events that they’re not automatically propagated anywhere - you have to add listeners to the components and deal with the events there or pass them on to some other method as plain old Java objects. That’s to say, there’s no built in event bus. A typical way of adding a listener in your case could be something like this:

public class GroupsConf extends VerticalLayout implements View, Button.ClickListener {
// ...
        boutons = new BoutonCreerSupprimer(this);
        vLayoutGroup.addComponent(boutons);
//...
    @Override
    public void buttonClick(Button.ClickEvent clickEvent) {
        Notification.show("foo");
    }
public class BoutonCreerSupprimer extends CustomComponent {
//...
   public BoutonCreerSupprimer(Button.ClickListener listener) {
        Button button = new Button();
        button.addClickListener(listener); // note: you shouldn't remove listeners in Vaadin 8
// ...        

-Olli

Thanks again Olli,
Effectively I thougth events were automatically propagated, I’m no more use (or use anymore) with this pattern… so READ AGAIN…:smiley:
Thanks a lot