Pawan Pullarwar
Pawan Pullarwar

Reputation: 3

Vaadin flow, trying to display Horizontal layout in Grid

I have written below code, where I want to add HorizontalLayout with TextBox and Button in Vaadin Grid. But when I run the code it is showing the output as

com.vaadin.flow.component.orderedlayout.HorizontalLayout@145a03ea com.vaadin.flow.component.orderedlayout.HorizontalLayout@1f7acb46

package com.packagename.myapp;

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

import com.vaadin.flow.component.Text;
import com.vaadin.flow.component.button.Button;
import com.vaadin.flow.component.grid.Grid;
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
import com.vaadin.flow.router.Route;

@Route("table")
public class TestTable extends VerticalLayout {
    private Grid<TblValue> grid = new Grid<>();
    public TestTable() {
        init();
    }

    private void init() {
        grid.addColumn(TblValue::getLayout);
        grid.setItems(createData());
        add(grid);
    }

    private List<TblValue> createData(){
        List<TblValue> data = new ArrayList<>();
        data.add(new TblValue());
        data.add(new TblValue());
        return data;
    }

    private class TblValue {
        HorizontalLayout layout = new HorizontalLayout();
        private Text txt = new Text("test string");
        private Button btn = new Button("Submit");
        public TblValue() {
            layout.add(txt, btn);
        }
        public HorizontalLayout getLayout() {
            return layout;
        }
        public void setLayout(HorizontalLayout layout) {
            this.layout = layout;
        }
    }
}

Upvotes: 0

Views: 345

Answers (1)

kscherrer
kscherrer

Reputation: 5766

As mentioned in the comments by Tatu Lund, the solution to your problem is to use grid.addComponentColumn(...) instead of grid.addColumn(...).

The addColumn(...) method will always show a String. If the valueprovider returns any Object, it will show its string value with String.valueOf(object). This is what you are seeing. com.vaadin.flow.component.orderedlayout.HorizontalLayout@145a03ea is what you get when you call String.valueOf(myHorizontalLayout).

addComponentColumn(...) on the other hand will take the returned HorizontalLayout of the valueProvider, and put the actual component into the cell which is what you wanted.

Upvotes: 1

Related Questions