带UIBinder的GWT CellTable

时间:2013-07-12 09:10:20

标签: java javascript gwt uibinder

我试图在点击页面上的按钮时显示一个非常简单的cellTable。 但是,无法渲染单元格。

提供以下代码段以获得更多理解:

preview.ui.xml文件

<g:HTMLPanel>
    <div class="{bundle.css.roundedBorder}">
        <table style='width:100%;'>
            <tr>
                <td>
                    <c:CellTable pageSize='15' ui:field='cellTable' width="100%">  
                    </c:CellTable> 
                </td>
            </tr>
        </table>
    </div>
</g:HTMLPanel>

Correspoding Java Class:

public class Preview extends Composite {
.
.
.   // other generic GWT code to bind UIBinder XML with this class
.
.
.

@UiField
CellTable<Contact> cellTable;

@UiHandler("button")
void handleClickOnSearchButton(ClickEvent e) {
    cellTable = configureCellTable();
}

private CellTable<Contact> configureCellTable() {
// The list of data to display.
  List<Contact> CONTACTS = Arrays.asList(new Contact("John", "123 Fourth Road"), new Contact("Mary", "222 Lancer Lane"), new Contact("Zander", "94 Road Street"));
// Create a CellTable.
  CellTable<Contact> table = new CellTable<Contact>();


  // Create name column.
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact contact) {
        return contact.name;
      }
    };




    // Create address column.
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() {
      @Override
      public String getValue(Contact contact) {
        return contact.address;
      }
    };

    // Add the columns.
    table.addColumn(nameColumn, "Name");
    table.addColumn(addressColumn, "Address");


    // Create a data provider.
    ListDataProvider<Contact> dataProvider = new ListDataProvider<Contact>();

    // Connect the table to the data provider.
    dataProvider.addDataDisplay(table);

 // Add the data to the data provider, which automatically pushes it to the widget.
    List<Contact> list = dataProvider.getList();
    for (Contact contact : CONTACTS) {
      list.add(contact);
    }


return table;
}



private static class Contact {
private final String address;
private final String name;

public Contact(String name, String address) {
  this.name = name;
  this.address = address;
}  } }

请指导!

谢谢,

阿克沙伊

1 个答案:

答案 0 :(得分:2)

您有两个不同的cellTable对象。一个是由UiBinder创建的,一个是在configureCellTable方法中创建的。

尝试在UiBinder文件中添加SimplePanel而不是表:

<td>
    <g:SimplePanel ui:field="simplePanel"/>
</td>

在Java代码中添加表格:

@UiField
SimplePanel simplePanel;
...
    private CellTable<Contact> configureCellTable() {
    ...
        simplePanel.add(table);
    }