La classe GWT seguente consente di aggiungere un handler per gli eventi di onclick, onmouseover e onmouseout per le righe di una flextable.
import com.google.gwt.user.client.Element;
import com.google.gwt.user.client.DOM;
import com.google.gwt.user.client.Event;
import com.google.gwt.user.client.EventListener;
public class FlexTableExt extends FlexTable {
public static interface RowEventsHandler {
public void onClick(FlexTableExt source, int row);
public void onMouseOver(FlexTableExt source, int row);
public void onMouseOut(FlexTableExt source, int row);
}
public void setRowEventsHandler(final int row, final RowEventsHandler handler){
Element rowEl = this.getRowFormatter().getElement(row);
DOM.setEventListener(rowEl, new EventListener(){
@Override
public void onBrowserEvent(Event event) {
switch(event.getTypeInt()){
case Event.ONCLICK:
handler.onClick(FlexTableExt.this, row); break;
case Event.ONMOUSEOVER:
handler.onMouseOver(FlexTableExt.this, row); break;
case Event.ONMOUSEOUT:
handler.onMouseOut(FlexTableExt.this, row); break;
}
}
});
DOM.sinkEvents(rowEl, Event.ONCLICK | Event.ONMOUSEOUT | Event.ONMOUSEOVER);
}
}
Example:
FlexTableExt ft = new FlextTableExt();
FlexTableExt.RowEventsHandler rowHandler = new FlexTableExt.RowEventsHandler(){
@Override
public void onClick(FlexTableExt ft, int row){
Window.alert("click row: "+row);
}
@Override
public void onMouseOver(FlexTableExt ft, int row){
ft.getRowFormatter().getElement(row).getStyle().setBackgroundColor("yellow");
}
@Override
public void onMouseOut(FlexTableExt ft, int row){
ft.getRowFormatter().getElement(row).getStyle().setBackgroundColor("");
}
};
for( int r=0; r<10; r++){
for(int c=0; c<5; c++){
ft.setText(r,c, "cell " + r + "," + c );
}
ft.setRowEventsHandler(r, rowHandler );
}