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.

Grid rows: how to add grid rows ?

App Keto
6 years ago Mar 15, 2016 5:34pm
Johannes Häyry
6 years ago Mar 16, 2016 7:13am
App Keto
6 years ago Mar 17, 2016 9:30am
Johannes Häyry
6 years ago Mar 17, 2016 9:39am
App Keto
6 years ago Mar 17, 2016 11:10am
Johannes Häyry
6 years ago Mar 17, 2016 11:35am
App Keto
6 years ago Mar 17, 2016 1:50pm
Johannes Häyry
6 years ago Mar 18, 2016 9:54am

I hope this helps.

package com.example.addingitemstogrid;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import javax.servlet.annotation.WebServlet;

import com.vaadin.annotations.Theme;
import com.vaadin.annotations.VaadinServletConfiguration;
import com.vaadin.data.Property;
import com.vaadin.data.util.BeanItemContainer;
import com.vaadin.server.VaadinRequest;
import com.vaadin.server.VaadinServlet;
import com.vaadin.ui.Button;
import com.vaadin.ui.Grid;
import com.vaadin.ui.HorizontalLayout;
import com.vaadin.ui.ListSelect;
import com.vaadin.ui.UI;
import com.vaadin.ui.VerticalLayout;

@SuppressWarnings("serial")
@Theme("valo")
public class AddingitemstogridUI extends UI {
    private static final int POJO_AMOUNT = 30;
    private Button addNew;
    private Button addSelected;
    private ListSelect select;
    private Grid grid;
    
    @WebServlet(value = "/*", asyncSupported = true)
    @VaadinServletConfiguration(productionMode = false, ui = AddingitemstogridUI.class)
    public static class Servlet extends VaadinServlet {
    }

    @Override
    protected void init(VaadinRequest request) {
        
        BeanItemContainer<MyPojo> selectContainer = new BeanItemContainer<>(MyPojo.class);
        BeanItemContainer<MyPojo> gridContainer = new BeanItemContainer<>(MyPojo.class);

        select = new ListSelect();
        select.setItemCaptionPropertyId("name");
        select.setMultiSelect(true);
        select.setContainerDataSource(selectContainer);
        select.addValueChangeListener(this::onListSelectValueChanged);
        selectContainer.addAll(getRandomPojos(POJO_AMOUNT));
        
        grid = new Grid();
        grid.setContainerDataSource(gridContainer);
        grid.getContainerDataSource().addItem(new MyPojo("Hello", 13));
        
        VerticalLayout buttons = new VerticalLayout();
        addNew = new Button("Add New", this::onAddNew);
        addNew.setWidth("100%");
        addSelected = new Button("Add Selected", this::onAddSelected);
        addSelected.setWidth("100%");
        addSelected.setVisible(false);
        buttons.addComponents(addSelected, addNew);
        buttons.setWidth("130px");
        buttons.setSpacing(true);
        
        HorizontalLayout content = new HorizontalLayout();
        
        setContent(content);
        content.addComponents(select, buttons, grid);
        content.setExpandRatio(grid, 1.0f);
        content.setSpacing(true);
        content.setMargin(true);
    }
    
    private void onAddNew(Button.ClickEvent event) {
        grid.getContainerDataSource().addItem(new MyPojo("Douglas Adams", 42));
    }
    
    private void onAddSelected(Button.ClickEvent event) {
        if (select.getValue() instanceof Collection) {
            for (Object itemId : ((Collection<?>) select.getValue())) {
                // In BeanItemContainer the item id is actually the bean. 
                // The item is BeanItem and we don't want that.
                // You could clone your bean here if you need another reference
                grid.getContainerDataSource().addItem(itemId);
            }
        }
    }
    
    private void onListSelectValueChanged(Property.ValueChangeEvent event) {
        if (((Collection<?>) event.getProperty().getValue()).isEmpty()) {
            addNew.setVisible(true);
            addSelected.setVisible(false);
        } else {
            addNew.setVisible(false);
            addSelected.setVisible(true);
        }
    }
    
    private static List<MyPojo> getRandomPojos(int amount) {
        Random rand = new Random(1001L);
        String names = { "John", "Johannes", "Jonathan", "Jonas", "Joan", "Jill" };
        int ages = { 21, 28, 32, 64, 35, 46 };
        
        List<MyPojo> result = new ArrayList<>();
        for (int i = 0; i < amount; i++) {
            result.add(new MyPojo(names[rand.nextInt(names.length)], ages[rand.nextInt(ages.length)]));
        }
        
        return result;
    }
    
    public static class MyPojo {
        private String name;
        private int age;
        
        public MyPojo() {
            
        }
        
        public MyPojo(String name, int age) {
            this.name = name;
            this.age = age;
        }
        
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            this.age = age;
        }
    }

}

EDIT 21.3.2016: fixed button visibility bugs

App Keto
6 years ago Mar 21, 2016 6:57am