Vaadin grid.setItems not working in propertyChange methode

177 Views Asked by At

I have created a vaadin application using vaadin 14, i have a class accordion that passes an object HandlerTest to class Grid using a propertyChange listener. The object do pass to the grid class but when i try to use grid.setItems to show it it doesn't work. grid.setItems does add the object if i add it from the constructer but don't if i add it from the propertyChange methode. Here is the code i used for bothe classes and the main class:

the Grid class: `

package org.vaadin.example;

import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.notification.Notification;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import java.util.List;

@Route("grid")
public final class GridClass extends VerticalLayout implements PropertyChangeListener {
    HandlerTest ht = new HandlerTest();
    List<HandlerTest> list = new ArrayList<>();
    Grid<HandlerTest> grid = new Grid<>();

    public GridClass() {

        grid.addColumn(HandlerTest::getPattern).setHeader("Pattern");
        grid.addColumn(HandlerTest::getModuleName).setHeader("Module");

        grid.setItems(list);
        grid.getDataProvider().refreshAll();
        grid.setAllRowsVisible(true);
        grid.setHeight("480px");
        add(grid);
    }

    @Override
    public void propertyChange(PropertyChangeEvent evt) {

        HandlerTest newHandler = (HandlerTest) evt.getNewValue();
        ht = newHandler;

        list.add(ht);

        Notification.show("Value of obj :" + evt.getNewValue());
        grid.getDataProvider().refreshAll();
    }
}

`

here is the HandlerTest class:

`

package org.vaadin.example;

public class HandlerTest implements java.io.Serializable{
    private String moduleName;
    private String pattern;
    private String method;
    private String sourceType;
    private String itemsPerPage;
    private String mimesAllowed;
    private String comments;
    private String pSource;

    public HandlerTest(String moduleName, String pattern, String method, String sourceType, String itemsPerPage, String mimesAllowed, String comments, String pSource) {
        this.moduleName = moduleName;
        this.pattern = pattern;
        this.method = method;
        this.sourceType = sourceType;
        this.itemsPerPage = itemsPerPage;
        this.mimesAllowed = mimesAllowed;
        this.comments = comments;
        this.pSource = pSource;
    }

    public HandlerTest() {
    }


    public String getModuleName() {
        return moduleName;
    }

    public void setModuleName(String moduleName) {
        this.moduleName = moduleName;
    }

    public String getPattern() {
        return pattern;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    public String getMethod() {
        return method;
    }

    public void setMethod(String method) {
        this.method = method;
    }

    public String getSourceType() {
        return sourceType;
    }

    public void setSourceType(String sourceType) {
        this.sourceType = sourceType;
    }

    public String getItemsPerPage() {
        return itemsPerPage;
    }

    public void setItemsPerPage(String itemsPerPage) {
        this.itemsPerPage = itemsPerPage;
    }

    public String getMimesAllowed() {
        return mimesAllowed;
    }

    public void setMimesAllowed(String mimesAllowed) {
        this.mimesAllowed = mimesAllowed;
    }

    public String getComments() {
        return comments;
    }

    public void setComments(String comments) {
        this.comments = comments;
    }

    public String getpSource() {
        return pSource;
    }

    public void setpSource(String pSource) {
        this.pSource = pSource;
    }

    @Override
    public String toString() {
        return moduleName + "/" + pattern + "/" + method + "/" + sourceType + "/" + itemsPerPage + "/" + mimesAllowed 
                + "/" + comments + "/" + pSource ;
    }
    
    public HandlerTest createHandlerFromString(HandlerTest h){
       
        String[] handlerText = (h.toString()).split("/");
        HandlerTest handler = new HandlerTest(handlerText[0], handlerText[1], handlerText[2], handlerText[3], 
                handlerText[4], handlerText[5], handlerText[6], handlerText[7]);

        return handler;
    }

}

`

i was expecting the grid to be refreshed and the item is added but nothing happened. The notification shows the object to display but the grid don't add it.

0

There are 0 best solutions below