Click Listener on CellTableRow

43 views
Skip to first unread message

Foermchen82

unread,
Aug 31, 2011, 3:11:54 PM8/31/11
to Google Web Toolkit
Hi,

I want to handle click events on cell table rows. But i'm not able to
do this!

I tried the following code:

CellTable<MyObject> table = new CellTable<MyObject>();
final SingleSelectionModel<MyObject> model = new
SingleSelectionModel<MyObject>();
table.setSelectionModel(model);

table.addHandler(new ClickHandler() {

@Override
public void onClick(ClickEvent event) {
try{
Window.alert(model.getSelectedObject());
}
catch(Exception e){
System.out.println(e);
}
}
}, ClickEvent.getType());

The problem is, that I try to get the object of the row on which I
have clicked. But on the click, the row under the mouse is actually
not selected. so the popup shows the wrong object.

How can I retreive the object under the mouse?

Kind regards,

Juan Pablo Gardella

unread,
Aug 31, 2011, 3:28:16 PM8/31/11
to google-we...@googlegroups.com
You can extend CellTable and overwritte onBrowserEvent2.

 @Override
  protected void onBrowserEvent2(Event event) {
    super.onBrowserEvent2(event);

    String eventType = event.getType();
    boolean isClick = "click".equals(eventType);

    // Get the event target.
    EventTarget eventTarget = event.getEventTarget();
    if (!Element.is(eventTarget)) {
      return;
    }
    final Element target = event.getEventTarget().cast();

    if (isClick) {
      int columnClicked = getCellIndex(target);
      boolean isExcludedColumn =
          this.excludedColumns != null
              && (Arrays.binarySearch(this.excludedColumns, columnClicked) >= 0);
      if (!"input".equalsIgnoreCase(target.getTagName())
          && !isExcludedColumn) {
        T item = getCurrentSelectedItem(target);
       //do something
      }
    }
  }

2011/8/31 Foermchen82 <Juerge...@gmx.de>

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


Foermchen82

unread,
Sep 1, 2011, 8:28:29 AM9/1/11
to Google Web Toolkit
You retreive the item by using the currentSelectedItem. But on click,
the row is actually not selected. So you will get the Item of the
previous selected row.

Is there not "normal" way for handling a click on a row of a
celltable?
On a flex table there is a "addTableListener" method. but this is
deprecated. On a HTMLTable there is the method "getcellforEvent". But
how I can do this with a CellTable??

I only want to have the index of the row that I have clicked.

Can somebody pleas help me????



On 31 Aug., 21:28, Juan Pablo Gardella <gardellajuanpa...@gmail.com>
wrote:
> You can extend CellTable and overwritte onBrowserEvent2.
>
>  @Override
>   protected void onBrowserEvent2(Event event) {
>     super.onBrowserEvent2(event);
>
>     String eventType = event.getType();
>     boolean isClick = "click".equals(eventType);
>
>     // Get the event target.
>     EventTarget eventTarget = event.getEventTarget();
>     if (!Element.is(eventTarget)) {
>       return;
>     }
>     final Element target = event.getEventTarget().cast();
>
>     if (isClick) {
>       int columnClicked = getCellIndex(target);
>       boolean isExcludedColumn =
>           this.excludedColumns != null
>               && (Arrays.binarySearch(this.excludedColumns, columnClicked)>= 0);
>
>       if (!"input".equalsIgnoreCase(target.getTagName())
>           && !isExcludedColumn) {
>         T item = getCurrentSelectedItem(target);
>        //do something
>       }
>     }
>   }
>
> 2011/8/31 Foermchen82 <Juergen.F...@gmx.de>

Thomas Broyer

unread,
Sep 1, 2011, 8:46:26 AM9/1/11
to google-we...@googlegroups.com
Can't you simply respond to SelectionChangeEvent-s instead?

Foermchen82

unread,
Sep 2, 2011, 8:02:43 AM9/2/11
to Google Web Toolkit
No,

because this will result in a mix of click and selection handling.
These are different functionalities for different usecases.

Thomas Broyer

unread,
Sep 2, 2011, 8:17:34 AM9/2/11
to google-we...@googlegroups.com
OK, then I'd say either use ClickableTextCell everywhere (or wrap each Cell into something similar to a ClickableTextCell, for those cells that are not "text based"), or try using a CellPreviewEvent.Handler (or rather, DefaultSelectionEventManager and EventTranslator).
You could also probably override onBrowserEvent of your Columns.

Foermchen82

unread,
Sep 2, 2011, 11:09:23 AM9/2/11
to Google Web Toolkit
Thank you for your answers.

My solution now is, that i have copied the functionality from the
HTMLTable. There is a method: getCellFromEvent. This is exactly what
i'm looking for.

Kind regards.
Reply all
Reply to author
Forward
0 new messages