17

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Opal
  • 81,889
  • 28
  • 189
  • 210

4 Answers4

26

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");

       }

    }
}
Eric
  • 471
  • 4
  • 9
5

You could use a stylename for the specific column, using the addColumnStyleName(int index, java.lang.String styleName) method.

Javadoc for CellTable

bestform
  • 353
  • 1
  • 7
  • I know about this method by don't know how to use it. Where such style class should be declared? – Opal Dec 16 '10 at 15:40
  • In your application's stylesheet. Have a look at [ClientBundle](http://code.google.com/intl/de-DE/webtoolkit/doc/latest/DevGuideClientBundle.html) or just include a custom stylesheet. – bestform Dec 16 '10 at 15:58
  • Thanks, it works. I would like to to declare this stylesheet in a CellTable.Resource but I get an Exception: [ERROR] Unable to process CSS. Do You know how it can be done? – Opal Dec 16 '10 at 16:09
  • Sorry, I haven't done this myself, yet, and your info about the error is a little too vague. – bestform Dec 16 '10 at 16:27
1

What worked for me is adding a new class in my css. This class gets applied only to select elements whose length varies depending on data.

.__gwt_cell select{
    width:170px;
}

Then applying it on my particular cell style like o:

table.getColumn(3).setCellStyleNames("yourstyle");
Adrian Sanguineti
  • 2,455
  • 1
  • 27
  • 29
Lux
  • 9
  • 3
-3

The GWT documentation for CellTable covers this, see: http://www.gwtproject.org/doc/latest/DevGuideUiCellTable.html

Under "Controlling Column Widths".