javagwtgwt-2.2-celltablecustom-cellcelltable

How would I create a custom GWT CellTable class?


I have a CellTable created in GWT that allows users to update/delete records in a database. This kind of table will be used in about 10 different places with very minimal changes (same number of columns, all with the same type, just need to be able to change the column header titles).

How would I go about creating a custom CellTable class? It'd be a shame to have to copy and paste boilerplate code everywhere.


Solution

  • In short, there is nothing preventing you from extending CellTable. It can be extended just like any other java class.

    For example, Let us use a CellTable of such:

    CellTable<Contact> table = new CellTable<Contact>();
    
    TextColumn<Contact> nameColumn = new TextColumn<Contact>() { /* ... */ };  
    table.addColumn(nameColumn, "Name");
    
    TextColumn<Contact> addressColumn = new TextColumn<Contact>() { /* ... */ };
    table.addColumn(addressColumn, "Address");
    
    table.setRowData( /* ... */ );
    

    You could extract it into your own class like so:

    public class MyCellTable extends CellTable<Contact>{
        public MyCellTable(String col1, String col2){
            TextColumn<Contact> nameColumn = new TextColumn<Contact>() { /* ... */ };  
            table.addColumn(nameColumn, col1);
    
            TextColumn<Contact> addressColumn = new TextColumn<Contact>() { /* ... */ };
            table.addColumn(addressColumn, col2);
        }
    }
    

    By extending the CellTable, you can use your cell table like any other.

    MyCellTable table = new MyCellTable("Name", "Address");
    table.setRowData( /* ... */ );