Archive for December, 2012

CustomCell with GWT to show multiple items in one DataGrid or CellTable cell

While working in GWT, I recently I had a need to show multiple items in one cell of a DataGrid.  The documentation was a little sketchy and I didn’t quite follow some of the postings out there on the topic.  However, they were helpful in nudging me along.  I ended up with the following sample.  Hopefully others will find it helpful.

I tried to stick closely to the documentation and my understanding of how cell widgets work.  Specifically, a row in a table (CellTable and DataGrid) is of one constant type, typically an object  (DTO), and a column represents on “field” or “attribute” of the DTO.  In this case, with a cell containing multiple items, the implication is the field is a list of some sort.

This example shows a list of Drivers and the cars they own.  A driver may own multiple cars.  One driver is displayed per row, with the cars shown in one cell.  When run, the application looks like this.


The data structures are as expected.  There is a many to one relationship between Drivers and Cars. Here they are, summarized.

public class Driver
 private int id;
 private String firstname;
 private String lastname;
 private String manufacturerPreference;
 private ArrayList<Car> owns = new ArrayList<Car>();

public class Car
 private int id;
 private String manufacturer;
 private String model;

In terms of implementation, the main thing I found confusing is the definition of the Column and Cell.  I’ve defined the Cars column as a list, many examples simply use a String to represent that cell. Here is what I ended up with.

private class DriverColumn extends TextColumn<Driver>
 public String getValue( Driver driver )
  return driver.getFirstname() + " " + driver.getLastname();

private class CarsCell extends AbstractCell<ArrayList<Car>>
 public CarsCell()
  super( "click" );

Once these definitions are established, the rest of the code makes sense – at least it seems more consistent with the documentation.

The full code can be downloaded from here. This is a full working example.

December 7, 2012 at 1:49 pm Leave a comment

RSS Twitter Timeline

  • An error has occurred; the feed is probably down. Try again later.
December 2012
« Mar   Sep »