Loading...
Important Notice - Forums is archived

To simplify things and help our users to be more productive, we have archived the current forum and focus our efforts on helping developers on Stack Overflow. You can post new questions on Stack Overflow or join our Discord channel.

Product icon
TUTORIAL

Vaadin lets you build secure, UX-first PWAs entirely in Java.
Free ebook & tutorial.

Difficulty creating a Custom Grid Renderer to mimic a TextField

Dan Golob
6 years ago Jul 07, 2015 2:18pm
Kim Leppänen
6 years ago Jul 07, 2015 2:31pm
Dan Golob
6 years ago Jul 07, 2015 2:49pm
Ilia Motornyi
6 years ago Jul 07, 2015 3:26pm
Dan Golob
6 years ago Jul 07, 2015 3:57pm
Dan Golob
6 years ago Jul 07, 2015 5:55pm
Dan Golob
6 years ago Jul 07, 2015 7:56pm
Ilia Motornyi
6 years ago Jul 08, 2015 8:25am
Dan Golob
6 years ago Jul 08, 2015 12:23pm
Ilia Motornyi
6 years ago Jul 08, 2015 12:53pm
Dan Golob
6 years ago Jul 08, 2015 1:36pm
Ilia Motornyi
6 years ago Jul 08, 2015 2:35pm
Dan Golob
6 years ago Jul 08, 2015 3:55pm
Marius Reinwald
6 years ago Jul 09, 2015 6:26am

To understand a bit more about custom renderers I tried to create a Renderer like you want one (if i didn't misunderstood your post) and i think i did it. I made a Renderer which display a gwt TextBox in each cell of the column and on Value change (so on enter or Tab, ..) it will make an rpc to the server side and change the value of the item property to the new value. I think i probably overcomplicated and i don't know if the RPC was really necessary or if it could've been done on the client side but here is the code. Hope it helps:

Server side:

import com.test.client.TextFieldValChangeRPC;
import com.vaadin.data.Container.Indexed;
import com.vaadin.ui.Grid.AbstractRenderer;

public class TextFieldRenderer<T> extends AbstractRenderer<T> {
    private static final long serialVersionUID = 1L;

    protected TextFieldRenderer(Class<T> arg0) {
        super(arg0);
        registerRpc(new TextFieldValChangeRPC() {
            private static final long serialVersionUID = 1L;

            @Override
            public void valChange(String propID, String index, String newValue) {
                Indexed cont = getParentGrid().getContainerDataSource();
                cont.getItem(cont.getItemIds().toArray()[Integer.valueOf(index)]).getItemProperty(cont.getContainerPropertyIds().toArray()[Integer.valueOf(propID)]).setValue(newValue);
            }
        });
    }
}

Client  side:

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.google.gwt.core.shared.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.event.shared.HandlerManager;
import com.google.gwt.user.client.ui.TextBox;
import com.vaadin.client.renderers.WidgetRenderer;
import com.vaadin.client.widget.grid.RendererCellReference;

public class TextFieldRenderer extends WidgetRenderer<String, TextBox> {

    @Override
    public TextBox createWidget() {
         TextBox txt = GWT.create(TextBox.class);
         txt.setStylePrimaryName("v-textfield");
         return txt;
    }

    @Override
    public void render(final RendererCellReference cell, String data, TextBox widget) {
        widget.setText(data);
        final int rowindex = cell.getRowIndex();
        widget.addValueChangeHandler(new ValueChangeHandler<String>() {
            
            @Override
            public void onValueChange(ValueChangeEvent<String> event) {
                fireValChange(cell.getColumnIndex()+"", rowindex+"", event.getValue()+"");
            }
        });
    }
    
    public interface ValChangeListener{
        public void valChange(Object propID, Object index, String newValue);
    }
    
    private List<ValChangeListener> listeners = new ArrayList<TextFieldRenderer.ValChangeListener>();
    public void addValChangeListener(ValChangeListener listener){
        listeners.add(listener);
    }
    public void removeValChangeListener(ValChangeListener listener){
        listeners.remove(listener);
    }
    
    public void fireValChange(Object propID, Object index, String newValue){
        for (Iterator it_listener = listeners.iterator(); it_listener.hasNext();) {
            ValChangeListener listener = (ValChangeListener) it_listener.next();
            listener.valChange(propID, index, newValue);
        }
    }

}
import com.test.client.TextFieldRenderer.ValChangeListener;
import com.vaadin.client.connectors.AbstractRendererConnector;
import com.vaadin.shared.ui.Connect;

@Connect(com.test.TextFieldRenderer.class)
public class TextFieldRendererConnector extends AbstractRendererConnector<String> {
     @Override
        public TextFieldRenderer getRenderer() {
            return (TextFieldRenderer) super.getRenderer();
        }
    
     ValChangeListener listener;
     @Override
    protected void init() {    
        getRenderer().addValChangeListener(listener = new ValChangeListener() {
            
            @Override
            public void valChange(Object propID, Object index, String newValue) {
                getRpcProxy(TextFieldValChangeRPC.class).valChange(propID+"", index+"", newValue);
            }
        });
        super.init();
    }
     @Override
    public void onUnregister() {
         getRenderer().removeValChangeListener(listener);
        super.onUnregister();
    }
}
import com.vaadin.shared.communication.ServerRpc;

public interface TextFieldValChangeRPC extends ServerRpc{
    public void valChange(String propID, String index, String newValue);
}

My test looks like:

Grid grid = new Grid();
        IndexedContainer cont = new IndexedContainer();
        cont.addContainerProperty("TEST", String.class, "testval");
        cont.addItem();
        cont.addItem();
        cont.addItem();
        cont.addItem();
        cont.addItem();
        cont.addItem();
        cont.addItem();
        cont.addItem();
        grid.setContainerDataSource(cont);
        grid.getColumn("TEST").setRenderer(new TextFieldRenderer(String.class));

I made these while compiling my main project so i can't guarantee that it works perfectly.

Ilia Motornyi
6 years ago Jul 09, 2015 11:52am
Dan Golob
6 years ago Jul 09, 2015 1:23pm
Marius Reinwald
6 years ago Jul 09, 2015 2:32pm
Ilia Motornyi
6 years ago Jul 09, 2015 2:40pm
Dan Golob
6 years ago Jul 09, 2015 2:41pm
Ilia Motornyi
6 years ago Jul 09, 2015 3:09pm
Dan Golob
6 years ago Jul 09, 2015 4:23pm
Dan Golob
6 years ago Jul 09, 2015 7:40pm
Marius Reinwald
6 years ago Jul 10, 2015 10:05am
Dan Golob
6 years ago Jul 10, 2015 7:49pm
Marius Reinwald
6 years ago Jul 13, 2015 1:17pm
Dan Golob
6 years ago Jul 16, 2015 6:37pm
Blanford Robinson
6 years ago Aug 10, 2015 12:48pm
Marius Reinwald
6 years ago Aug 19, 2015 2:25pm
Dan Golob
6 years ago Aug 19, 2015 2:29pm
Ynag yo
5 years ago Oct 18, 2016 6:44am
Tatu Lund
5 years ago Oct 18, 2016 9:40am
yentrang korea
5 years ago Oct 19, 2016 10:32am
Esau Betancourt
5 years ago Nov 02, 2016 9:21pm
Tatu Lund
5 years ago Nov 03, 2016 6:31am
Esau Betancourt
5 years ago Nov 03, 2016 9:20pm
Dan Golob
5 years ago Nov 04, 2016 12:19pm
Dan Golob
5 years ago Nov 04, 2016 12:22pm
Esau Betancourt
5 years ago Nov 04, 2016 9:11pm
Dan Golob
5 years ago Nov 07, 2016 1:46pm
Tatu Lund
5 years ago Nov 07, 2016 1:49pm