Remove Click Handler-GWT - 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 );
}
} );
}

Related

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");

Fire a (KEY_ENTER) key press event in GWT

What I'm trying to do is fire an enter key press event in GWT.
This is my keyhandler:
itemBox.addKeyDownHandler(new KeyDownHandler() {
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode == KeyCodes.KEY_ENTER) {
// do something
}
Then later I wanna fire an enter key press event but I can't seem to figure out how I do this. I wanna do something like KeyDownEvent.setNativeKeyCode(KEY_ENTER).
textBox.fireEvent(new KeyDownEvent(null));
Is it possible to set these parameters?
You can fire the event using DomEvent.fireNativeEvent, instead of textBox.fireEvent.
Here is a working example how to do this:
final TextBox tb = new TextBox();
tb.addKeyDownHandler(new KeyDownHandler() {
#Override
public void onKeyDown(KeyDownEvent event) {
if(event.getNativeKeyCode() == KeyCodes.KEY_ENTER) {
Window.alert("enter!");
}
}
});
Button b = new Button("keyevent");
b.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent event) {
DomEvent.fireNativeEvent(Document.get().createKeyDownEvent(false, false, false, false, KeyCodes.KEY_ENTER), tb);
}
});
RootPanel.get().add(tb);
RootPanel.get().add(b);

How to capture doubleClickEvent in GWT CellTable

I'm trying to make a GWT CellTable catch events of type DoubleClickEvent, but while the CellTable correctly receives events of type ClickEvent when a row is clicked in the UI, it not see any DoubleClickEvent when the row is double-clicked.
So, if I click a row in the UI, the handler declared for ClickEvent is correctly triggered, but if I double click the handler declared for DoubleClickEvent is not triggered, instead.
Am I doing something wrong or CellTable itself cannot handle DoubleClickEvent at all?
In the latter case, what could be a good way to capture double-clicks in a table?
Below, the code for my CellTable declaration:
CellTable<ServiceTypeUI> contentTable = new CellTable<ServiceTypeUI>(10, style);
contentTable.setSelectionModel(new SingleSelectionModel<ServiceTypeUI>());
contentTable.addHandler(new DoubleClickHandler() { // HANDLER NOT CORRECTLY TRIGGERED
#Override
#SuppressWarnings("unchecked")
public void onDoubleClick(DoubleClickEvent event) {
presenter.doubleClickHandler(event);
}
}, DoubleClickEvent.getType());
contentTable.addHandler(new ClickHandler() { // HANDLER CORRECTLY TRIGGERED
#Override
#SuppressWarnings("unchecked")
public void onClick(ClickEvent event) {
presenter.clickHandler(event);
}
}, ClickEvent.getType());
I've also tried removing ClickEvent handler declaration and the SelectionModel declaration, to avoid that any of those capture the DoubleClickEvent event and treat it as a ClickEvent but the DoubleClickHandler has not been triggered even in this case.
CellTable<ServiceTypeUI> contentTable = new CellTable<ServiceTypeUI>(10, style);
contentTable.addHandler(new DoubleClickHandler() { // HANDLER NOT CORRECTLY TRIGGERED
#Override
#SuppressWarnings("unchecked")
public void onDoubleClick(DoubleClickEvent event) {
presenter.doubleClickHandler(event);
}
}, DoubleClickEvent.getType());
SingleSelectionModel<T> selectionModel
= new SingleSelectionModel<T>();
cellTable.setSelectionModel(selectionModel);
cellTable.addDomHandler(new DoubleClickHandler() {
#Override
public void onDoubleClick(final DoubleClickEvent event) {
T selected = selectionModel
.getSelectedObject();
if (selected != null) {
//DO YOUR STUFF
}
}
},
DoubleClickEvent.getType());
You have to replace the T with the your "ServiceTypeUI" . The value selected will be the object which was been chosen from the user.

Capturing mouse events on horizontal panel in 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
}
});

GWT: How to disable the anchor link event when clicked

I want to disable the anchor link event when it clicked one time. I used anchor.setenabled(false) but nothing happend. When I click the same button again the event e is true. I want false at that time.
public void onCellClick(GridPanel grid, int rowIndex, int colindex,EventObject e)
{
if(rowIndex==0 && colindex==2){
tomcatHandler = "Start";
anchorStart.setEnabled(false);
}else if(rowIndex==0 && colindex==3){
tomcatHandler = "Stop";
****anchorStop.setEnabled(false);
anchorStart.setEnabled(false);
anchorRestart.setEnabled(true);****
}else if(rowIndex==0 &&colindex==4){
tomcatHandler = "Restart";
anchorRestart.setEnabled(false);
}
AdminService.Util.getInstance().tomcat(tomcatHandler,new AsyncCallback<String>() {
#Override
public void onSuccess(String result) {
imageChangeEvent(result);
}
#Override
public void onFailure(Throwable caught) {
}
});}
Anchors in GWT have always had a problem with setEnabled() because HTML doesn't support such a property. A quick workaround is to create a new widget that subclasses GWT's Anchor, adding the following override:
#Override
public void onBrowserEvent(Event event) {
switch (DOM.eventGetType(event)) {
case Event.ONDBLCLICK:
case Event.ONFOCUS:
case Event.ONCLICK:
if (!isEnabled()) {
return;
}
break;
}
super.onBrowserEvent(event);
}
This disables the passing of the browser event to GWT's Anchor class (summarily disabling all related handlers) when the link is double clicked, focused or clicked and is in a disabled state.
Source
It doesn't seem to actually disable the anchor, but it does retain the status that has been set with anchor.setEnabled(), so just test that within your handler e.g.
myAnchor.addClickHandler(new ClickHandler() {
#Override
public void onClick(ClickEvent evt) {
// write to filter and then call reload
if (((Anchor) evt.getSource()).isEnabled()) {
//do stuff
}
}
});