GWT:将带处理程序的Image添加到表头

时间:2012-07-28 10:58:11

标签: java gwt

我在GWT中遇到了一些CellTable的问题。

我想做什么: 我有CellTable,我想添加带有图像的标题并添加动作。但它在我的代码中不起作用。

我的代码(全部):

import java.util.Arrays;
import java.util.List;

import com.google.gwt.cell.client.Cell;
import com.google.gwt.cell.client.ClickableTextCell;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.safehtml.shared.SafeHtmlBuilder;
import com.google.gwt.safehtml.shared.SafeHtmlUtils;
import com.google.gwt.user.cellview.client.CellTable;
import com.google.gwt.user.cellview.client.Header;
import com.google.gwt.user.cellview.client.TextColumn;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.VerticalPanel;`

public class HelloGWT implements EntryPoint {

    public void onModuleLoad() {

        RootPanel.get().add(new MyTable());

    }
}

class Contact {
    public Contact(String name, int number) {
        this.number = number;
        this.name = name;
    }

    public String name;
    public int number;
}

class MyTable extends VerticalPanel {

    public List<Contact> list = Arrays.asList(new Contact("contact_1", 1),
            new Contact("contact_2", 2), new Contact("contact_3", 3)

    );

    public MyTable() {
        CellTable<Contact> table = new CellTable<Contact>();
        table.addColumn(new MyColumn(), new MyHeader(new ClickableTextCell(), "MyHeader!"));
        table.setRowData(0, list);

        this.add(table);
    }
}

class MyColumn extends TextColumn<Contact> {

    public MyColumn() {

    }

    @Override
    public String getValue(Contact object) {
        // TODO Auto-generated method stub
        return object.name;
    }
}

class MyHeader extends Header<String> {

    private String columnName;
    private Image image = new Image("http://www.google.com/mobile/images/mgc3/google-mobile-app48.png");

    public MyHeader(Cell<String> cell, String columnName) {
        super(cell);
        // TODO Auto-generated constructor stub
        this.columnName = columnName;

        image.addClickHandler(new ClickHandler()
        {

            @Override
            public void onClick(ClickEvent event) {
                System.out.println("Click!");

            }

        }
                );
    }

    @Override
    public String getValue() {
        // TODO Auto-generated method stub
        return columnName;
    }

    @Override
    public void render(Cell.Context context, SafeHtmlBuilder sb) {
        StringBuilder s = new StringBuilder();
        s.append(columnName);
        image.setSize("20px", "20px");
        s.append(image);
        sb.append(SafeHtmlUtils.fromSafeConstant(s.toString()));
    }

}

感谢任何想法...

0 个答案:

没有答案