tim_a
tim_a

Reputation: 950

Cellbrowser click event on cell

I can't figure out how I can add a click handler on a cell in a cellbrowser in GWT. I found another question here on StackOverFlow related to my question, but it was for a double click handler. I can't figure out how to add just a normal click handler.

My purpose is when a user clicks on a cell in the cellbrowser it downloads the child notes from a server. I already played around with onBrowserEvent but I could not get it work.

Upvotes: 2

Views: 2343

Answers (3)

tim_a
tim_a

Reputation: 950

Maybe this is also interesting for other web developers using GWT so this is how I did it:

// Create a clickable cell.
            Cell<C> cell = new ClickCell() {
              @Override
              public void render(Context context, C value, SafeHtmlBuilder sb) {
                if (value != null) {
                  sb.appendEscaped(value.getName());
                }
              }
              @Override
              public void onBrowserEvent(Context context, Element parent, C value,
                  NativeEvent event, ValueUpdater<C> valueUpdater) {
                super.onBrowserEvent(context, parent, value, event, valueUpdater);
                if ("click".equals(event.getType())) {
                  onEnterKeyDown(context, parent, value, event, valueUpdater);

                }
              }
            @Override
              protected void onEnterKeyDown(Context context, Element parent, C value,
                  NativeEvent event, ValueUpdater<C> valueUpdater) {
                if (valueUpdater != null) {
                  valueUpdater.update(value);
                }
              }
            }; 

And this is the class ClickCell that extends the AbstractCell class:

public abstract class ClickCell extends AbstractCell<C> {

    public ClickCell() {
        super("click", "keydown");
    }
}

Upvotes: 2

Thomas Broyer
Thomas Broyer

Reputation: 64541

In your TreeViewModel, you give a SelectionModel to the returned NodeInfo. You can listen for SelectionChangeEvents on the SelectionModel (and given your use case, you'll likely use a NoSelectionModel, most probably a single one shared by all levels that need that onclick behavior)

Upvotes: 3

Riley Lark
Riley Lark

Reputation: 20890

You can't add a ClickHandler like normal because a Cell is not a Widget. AbstractCell does have some methods that make it convenient for you to handle events, but you have to call its constructor with the names of the events you'd like to listen to. For instance, you'd pass "click" to the constructor of your cell, override onBrowserEvent, and check for "click" events there.

Look at the source for the ClickableTextCell to see how Google added click listeners to a cell.

Upvotes: 2

Related Questions