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开发者_StackOverflow properly. Is there a way to do it without using JSNI?
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!!!
This worked for me (Obviously, substitute "YOUR CLICKHANDLER" with your clickhandler's name) :
FlowPanel field = new FlowPanel();
field.addDomHandler(YOUR CLICKHANDLER, ClickEvent.getType());
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());
}
}
Seems question is a dumb one, yes. FlowPanel
can be easily wrapped or replaced with FocusPanel
, which provides a lot of method for monitoring/handling events.
Just wrap the FlowPanel with a FocusPanel, that will leave ready the widget for other user interaction too:
Source:
FlowPanel flowPanel = new FlowPanel();
FocusPanel focusPanel = new FocusPanel(flowPanel);
focusPanel.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
Window.alert("Hi!");
}
});
精彩评论