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?
This worked for me (Obviously, substitute "YOUR CLICKHANDLER" with your clickhandler's name) :
FlowPanel field = new FlowPanel();
field.addDomHandler(YOUR CLICKHANDLER, ClickEvent.getType());
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.
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!!!
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());
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With