admin管理员组

文章数量:1022944

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.

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.

Share Improve this question edited May 23, 2017 at 9:58 CommunityBot 11 silver badge asked Apr 6, 2011 at 18:11 tim_atim_a 9601 gold badge7 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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)

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.

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

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.

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.

Share Improve this question edited May 23, 2017 at 9:58 CommunityBot 11 silver badge asked Apr 6, 2011 at 18:11 tim_atim_a 9601 gold badge7 silver badges21 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 3

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)

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.

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

本文标签: javaCellbrowser click event on cellStack Overflow