Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the way to handle Click on GWT FlowPanel

May be a dumb question, but GWT FlowPanel (raw div element) does not provide something to handle a mouseclick/mousemovement on it. Overriding onBrowserEvent does not work either.

If setting onclick event using native JavaScript (need to specify positive height before, 'div' have a height of 0 if not specified), then catching these events is working properly. Is there a way to do it without using JSNI?

like image 685
shaman.sir Avatar asked Jun 08 '10 10:06

shaman.sir


4 Answers

This worked for me (Obviously, substitute "YOUR CLICKHANDLER" with your clickhandler's name) :

FlowPanel field = new FlowPanel();

field.addDomHandler(YOUR CLICKHANDLER, ClickEvent.getType());
like image 91
Churro Avatar answered Oct 16 '22 16:10

Churro


What you need to do here is wrap your FlowPanel in a FocusPanel. A FocusPanel contain all possible handler and thus will enable you to have a ClickHandler set to it.

Another method would be to create your own widget extending the flowpanel and implementing the necessary interface in order to be able to contain a ClickHandler.

I personally would recommend the first method. It's simpler, faster to code and won't slow down your application.

like image 29
Zwik Avatar answered Oct 16 '22 15:10

Zwik


Actually, you go for this:

FlowPanel fPanel = new FlowPanel() {
      @Override
      public void onAttach() {
           super.onAttach();
           super.addDomHandler(handler, ClickEvent.getType());  // handler is the instance         
                                                                // of your ClickHandler
      }
}

Cheers!!!

like image 8
BloodyJoez Avatar answered Oct 16 '22 16:10

BloodyJoez


The best solution I found for this was here.

The idea is to extend the FlowPanel (or indeed SimplePanel) to implement the relevant handler interfaces, which is a much neater option for situations where you do not require the full functionality and focus capabilities of the FocusPanel. See below:

public class MouseAwareFlowPanel extends FlowPanel implements 
HasMouseOverHandlers, HasMouseOutHandlers, HasClickHandlers 
{ 
    public HandlerRegistration addMouseOverHandler(MouseOverHandler handler) 
    { 
        return addDomHandler(handler, MouseOverEvent.getType()); 
    } 
    public HandlerRegistration addMouseOutHandler(MouseOutHandler handler) 
    { 
        return addDomHandler(handler, MouseOutEvent.getType()); 
    } 
    public HandlerRegistration addClickHandler(ClickHandler handler) 
    { 
        return addDomHandler(handler, ClickEvent.getType()); 
    } 
} 
like image 6
Nick Evans Avatar answered Oct 16 '22 15:10

Nick Evans