GWT CellTable - set column width

Opal picture Opal · Dec 16, 2010 · Viewed 21.7k times · Source

Is it possible to set the column width of CellTable in GWT?

Answer

Eric picture Eric · Dec 17, 2010

EDIT: As of GWT 2.2 table.setWidth and table.setColumnWidth are supported

table.setWidth("100%", true);
table.setColumnWidth(nameColumn, 35.0, Unit.PCT);
table.setColumnWidth(addressColumn, 65.0, Unit.PCT);

I was able to extend the CellTable with a method that sets the widths programmatically. It's a bit of a hack since all the real methods that should do this are private to CellTable and it seems like GWT should provide this method directly, but it seems to work.

public void setColumnWidths(List<Integer> widths)
{
    TableElement tel = TableElement.as(getElement());
    NodeList<Element> colgroups = tel.getElementsByTagName("colgroup");
    if (colgroups.getLength() == 1)
    {
       TableColElement cge = TableColElement.as(colgroups.getItem(0));
       NodeList<Element> cols = cge.getElementsByTagName("col");
       for (int j = 0; j < widths.size(); j++)
       {
           TableColElement column = null;
           if (cols.getLength() > j)
           {
               column = TableColElement.as(cols.getItem(j));
           }
           else
           {
               column = cge.appendChild(Document.get().createColElement());
           }

           column.setWidth(widths.get(j)+"px");

       }

    }
}