Saturday, July 28, 2012

GWT: adding Image with handler to Table Header

I've got some problems with CellTable in GWT.

What I want to do:
I've got CellTable and I want to add header with an image and add action to it. But it doesn't work in my code.

My code (all):

    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()));
        }
   
    }

Thanks for any ideas...

--
You received this message because you are subscribed to the Google Groups "Google Web Toolkit" group.
To view this discussion on the web visit https://groups.google.com/d/msg/google-web-toolkit/-/fIzNiYZ86RwJ.
To post to this group, send email to google-web-toolkit@googlegroups.com.
To unsubscribe from this group, send email to google-web-toolkit+unsubscribe@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/google-web-toolkit?hl=en.

No comments:

Post a Comment