Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT CellTable - set column width

Tags:

width

cell

gwt

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

like image 610
Opal Avatar asked Dec 16 '10 14:12

Opal


3 Answers

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

       }

    }
}
like image 200
Eric Avatar answered Nov 05 '22 12:11

Eric


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

Javadoc for CellTable

like image 5
bestform Avatar answered Nov 05 '22 11:11

bestform


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");
like image 1
Lux Avatar answered Nov 05 '22 11:11

Lux