Capturing mouse events on horizontal panel in GWT - gwt

Is there a way to capture mouse events on a horizontal panel in gwt ?
I am trying to capture mousedown or onclick events but m not able to get it working. This is what i have done so far
final HorizontalPanel container = new HorizontalPanel() {
#Override
public void sinkEvents(int eventBitsToAdd) {
// TODO Auto-generated method stub
super.sinkEvents(Event.ONCLICK);
}
#Override
public void onBrowserEvent(final Event event) {
// TODO Auto-generated method stub
super.onBrowserEvent(event);
if (DOM.eventGetType(event) == Event.ONCLICK) {
System.out.println("event type -->> " + event.getType());
}
/*if(Event.ONMOUSEDOWN == arg0.getTypeInt())
System.out.println("event type -->> " + arg0.getType());*/
}
};
I have no clue why this doesn't work.
any help would be appreciated.
Thanks

Instead of manually sinking and reading events, you could consider using Widget#addDomHandler():
HorizontalPanel container = new HorizontalPanel();
ClickHandler cHandler = new ClickHandler(){ /* ... */ };
MouseDownHandler mdHandler = new MouseDownHandler(){ /* ... */ };
container.addDomHandler(cHandler, ClickEvent.getType());
container.addDomHandler(mdHandler, MouseDownEvent.getType());

You can use FocusPanel. For example:
HorizontalPanel yourContainer = new HorizontalPanel();
FocusPanel wrapper = new FocusPanel();
wrapper.add(yourContainer );
wrapper.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// Handle the click
}
});

Related

GWT : Return VerticalPanel from a function in ClickHandler

I am trying to get a pop up when an image is clicked. below is the piece of code:
while(index<bookList.size()){
if(cellCount<4){
image[index] = new Image(imageList.get(index));
final int imageIndex = index;
table.setWidget(row, cellCount, new Image(bookList.get(index)));
table.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
VerticalPanel vPanel = new VerticalPanel();
vPanel = imageDetail.getPopup(popup, image[imageIndex]);
popup.setAnimationEnabled(true);
popup.setGlassEnabled(true);
popup.setAutoHideEnabled(false);
popup.setWidth("300px");
popup.setHeight("300px");
popup.add(vPanel);
popup.center();
}});
table.getFlexCellFormatter().setStyleName(row, cellCount, "ImageCell");
cellCount++;
index++;
}
Above code contain clickhandler which calls "getPopup" of imageDetail object. "getPopup" function returns a vertical Panel which is added to PopupPanel. ImageDetail class is as follows
public class ImageDetails extends Composite {
private Image closeButton;
VerticalPanel getPopup(final PopupPanel popup, Image image){
VerticalPanel vPanel = new VerticalPanel();
//Close Button
HorizontalPanel closePanel = new HorizontalPanel();
closeButton = new Image("/Images/closebutton.jpg");
closeButton.addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
popup.hide();
}});
closePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_RIGHT);
closePanel.add(closeButton);
vPanel.add(closePanel);
VerticalPanel imagePanel = new VerticalPanel();
imagePanel.add(image);
vPanel.add(imagePanel);
return vPanel;
}
My questions are as following :
1) Is it ok to return panels from a function? If no, then why? If yes, why above code gives "Uncaught exception escaped com.google.gwt.event.shared.UmbrellaException: 3 exceptions caught: null; null; null"?
2) When i try to add vertical panel to popup panel in "getPopup" function and get popup panel from there, code returning same error as mentioned in first question. What am I missing?
Am i missing any initWidget(w) function call?
Thank you

onMouseOver event on an image array is getting called only once in gwt

I have an image array attached to a DeckPanel. What I want is to have a pop-up on mouse hover on images which vanishes on mouseOut.
Code is as follows :
final PopupPanel popup = new PopupPanel();
for(int i=0; i<images.size();++i){
final int index = i;
image[i] = new Image(images.get(i));
image[i].addMouseOutHandler(new MouseOutHandler(){
#Override
public void onMouseOut(MouseOutEvent event) {
// TODO Auto-generated method stub
popup.hide();
}});
image[i].addMouseOverHandler(new MouseOverHandler(){
#Override
public void onMouseOver(MouseOverEvent event) {
// TODO Auto-generated method stub
Label label = new Label(images.get(index));
popup.add(label);
popup.showRelativeTo(image[index]);
}});
image[i].addClickHandler(new ClickHandler(){
#Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
Window.alert("image clicked");
}});
deck.add(image[i]);
}
Problem : onMouseOver() is getting only once. Could you guys please help me understand whats happening here and how tell me how to make it work for all images every time dat image is loaded?
onMouseOver you are repeatedly adding label to the popup.PopupPanel is a SimplePanel it can add only one widget.So onMouserOver first clear the PopupPanel
popup.clear();
Label label = new Label(images.get(index));
popup.add(label);
popup.showRelativeTo(image[index]);

When is Widget OnUnload() is called?

Can you please tell me when a widget's OnUnload() is called?
I tried to override it however, it is never been accessed. Also what is the best way to unload a composite, I am using RootPanel.get("Dev1").clear();
If you want to stop the timer when you leave the page then use CloseHandler.
This handler is called while page closing and refreshing.
Window.addCloseHandler(new CloseHandler<Window>() {
#Override
public void onClose(CloseEvent<Window> event) {
timer.cancel();
}
});
If you want to stop the timer when clearing a vertical panel then use removeFromParent.
This overridden method is to be called when any widget is removed form its parent.
private Timer timer = null;
/*
* This is the entry point method.
*/
public void onModuleLoad() {
final Label label = new Label("Hello ") {
#Override
public void removeFromParent() {
if (timer != null && this.isAttached()) {
timer.cancel();
System.out.println("timer is stopped");
}
super.removeFromParent();
}
};
timer = new Timer() {
#Override
public void run() {
label.setText("Hello " + (int) (Math.random() * 100));
}
};
timer.scheduleRepeating(500);
final VerticalPanel verticalPanel = new VerticalPanel();
verticalPanel.add(label);
Button button = new Button("Remove Label");
button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
label.removeFromParent();
}
});
verticalPanel.add(button);
RootPanel.get().add(verticalPanel);
}
As per you comments try below code where clear method is overridden for VerticalPanel.
final VerticalPanel verticalPanel = new VerticalPanel(){
#Override
public void clear(){
if(this.isAttached()){
timer.cancel();
}
super.clear();
}
};
verticalPanel.getElement().setId("Div1");

Adding a click handler to a panel?

We have the interface:
AbsolutePanel ap = ...;
ap.addHandler(EventHandler h, Type<EventHandler> type);
How do we use the above? It seems like the interface for click events has changed a few times in GWT, and older questions on this topic are out of date,
Thanks
This should work:
AbsolutePanel ap = new AbsolutePanel();
ap.addHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
}
}, ClickEvent.getType());

Remove Click Handler-GWT

How to remove the ClickHandler Event in GWT? I added addClickHandler() Event for a button and i want to remove the ClickHandler Event.I tried HandlerRegistration Method But it failed to remove the handler ,Here is a snippet :
notification.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// TODO Auto-generated method stub
}
});
I want to remove the handler with notification !
Note:
Notification is the button instance that calls the handler!
Each add...Handler method returns the HandlerRegistration interface. This interface contains the removeHandler() method. If you want to remove handlers, simple store the returned interface in a variable and call removeHandler when you want to remove the handler.
HandlerRegistration handler;
handler = button.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
// ...
}
});
handler.removeHandler();
This worked for me, I get the Handler registration when I bind the event,
closeIconHandlerRegistration = closeImg.addClickHandler( new ClickHandler()
{
#Override
public void onClick( ClickEvent event )
{
addCloseClickHanlder();
}
} );
After that When I need to remove the handler...
if ( this.getCloseButtonHandlerRegistration() != null )
{
this.getCloseButtonHandlerRegistration().removeHandler();
this.getCloseImg().addClickHandler( new ClickHandler()
{
#Override
public void onClick( ClickEvent event )
{
SaveCancelCommissionChangeEvent saveEvt = new SaveCancelCommissionChangeEvent();
saveEvt.setSave( false );
tabEventBus.fireEvent( saveEvt );
}
} );
}