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.

How to get the current bean being edited in a grid

Alisa Lee
5 years ago Oct 18, 2016 3:12pm
Goran Atanasovski
5 years ago Oct 27, 2016 12:25pm

Hi Alisa,

One way to retrieve the row's data would be from the commit handler such as:

Grid grid = new Grid();

grid.addColumn("Name", String.class);
grid.addColumn("Num", Integer.class);

grid.addRow("Nicolaus Copernicus", 1543);
grid.addRow("Galileo Galilei", 1564);
grid.addRow("Johannes Kepler", 1571);

grid.setEditorEnabled(true);

grid.getEditorFieldGroup().addCommitHandler(new CommitHandler() {

  @Override
  public void preCommit(CommitEvent commitEvent) throws CommitException {
    Item item = commitEvent.getFieldBinder().getItemDataSource();
    String name = (String) item.getItemProperty("Name").getValue();
    Integer num = (Integer) item.getItemProperty("Num").getValue();

    System.out.println("Pre commit values: " + name + " " + num);
  }

  @Override
  public void postCommit(CommitEvent commitEvent) throws CommitException {
    Item item = commitEvent.getFieldBinder().getItemDataSource();
    String name = (String) item.getItemProperty("Name").getValue();
    Integer num = (Integer) item.getItemProperty("Num").getValue();

    System.out.println("Post commit values: " + name + " " + num);
  }
});

Additonally, if you are using a BeanItemContainer, then you can also get the bean through the commit event. For example, if we have the following bean object:   

public class GridExampleBean {

        private String name;
        private int count;

        public GridExampleBean(String name, int count) {
            this.name = name;
            this.count = count;
        }

        public String getName() {
            return name;
        }

        public int getCount() {
            return count;
        }

        public void setName(String name) {
            this.name = name;
        }

        public void setCount(int count) {
            this.count = count;
        }

        @Override
        public String toString() {
            return String.format("Name: %s, count: %s", name, count);
        }
    }

Then in the commit event handler, you can access the bean itself, such as:

//...grid is created and populated

grid.getEditorFieldGroup().addCommitHandler(new CommitHandler() {

  @Override
  public void preCommit(CommitEvent commitEvent) throws CommitException {
    BeanItem item = (BeanItem) commitEvent.getFieldBinder().getItemDataSource();
    GridExampleBean bean = (GridExampleBean) item.getBean();

    System.out.println("Pre commit: " + bean.toString());
  }

  @Override
  public void postCommit(CommitEvent commitEvent) throws CommitException {
    BeanItem item = (BeanItem) commitEvent.getFieldBinder().getItemDataSource();
    GridExampleBean bean = (GridExampleBean) item.getBean();

    System.out.println("Post commit: " + bean.toString());
  }
});

Hope this helps,
Goran

Alisa Lee
5 years ago Oct 28, 2016 8:45pm
Daniel Stock
6 months ago Apr 11, 2021 5:38pm