I'm creating a web application with GWT. I have a method bookMode() that builds a UI with some textboxes, listboxes, etc. and shows it in a dialog box. I call the method like this:
//listen for mouse events on the add button
addButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String check = Cookies.getCookie("role");
if(check == "TopBeheerder") {
boolean currentMode = mode0.getValue();
add addObject = new add();
if(currentMode == true) {
addObject.bookMode();
}
if(currentMode == false) {
addObject.userMode();
}
}
else {
BibPhp.notification("You don't have enough permissions.");
}
}
});
When I use the GWT.log() function I notice that this method is called twice. But I searched all my code with the eclipse search function and the method isn't called twice. I have no idea why GWT behaves likes this. The method userMode() is also called twice.
I'm creating a web application with GWT. I have a method bookMode() that builds a UI with some textboxes, listboxes, etc. and shows it in a dialog box. I call the method like this:
//listen for mouse events on the add button
addButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String check = Cookies.getCookie("role");
if(check == "TopBeheerder") {
boolean currentMode = mode0.getValue();
add addObject = new add();
if(currentMode == true) {
addObject.bookMode();
}
if(currentMode == false) {
addObject.userMode();
}
}
else {
BibPhp.notification("You don't have enough permissions.");
}
}
});
When I use the GWT.log() function I notice that this method is called twice. But I searched all my code with the eclipse search function and the method isn't called twice. I have no idea why GWT behaves likes this. The method userMode() is also called twice.
Share Improve this question asked Feb 19, 2011 at 19:17 tim_atim_a 9501 gold badge7 silver badges21 bronze badges 5- Just to clarify, the behaviour you're experiencing is that the click handler code is executed twice for each "onclick" event? – Peter Commented Feb 19, 2011 at 19:23
- yes when i click on addButton it executes the method twice – tim_a Commented Feb 19, 2011 at 19:30
- 2 Okay, well there doesn't appear to be anything wrong. I would just make sure that the event handler isn't being inadvertently added twice, i.e. ensure there aren't two calls to addClickHandler() somehow. Put a Window.alert() just before you call addClickHandler() to confirm. – Peter Commented Feb 19, 2011 at 19:40
- Ok I've added a Window.alert() in the onClick method and i get the alert twice, and I click just once. I have no idea why it its like this. I also have the same problem on other buttons but not on all. There are also certain buttons where I have to click twice in order to call the event. – tim_a Commented Feb 19, 2011 at 19:49
- Put the alert just below addButton.addClickHandler(...) and see if it is once or if it is called twice. – Brad Commented Apr 11, 2011 at 16:17
5 Answers
Reset to default 5When the Java code gets converted to HTML and Javascript; the equality parison between java and javascript are not the same. Hence if you have two widgets (buttons) with the same id in HTML and add click handlers to each one by one, chances are high that the same widget gets the click handler added twice. Try setting different ids to the button widgets, that has helped me to resolve any issues related multiple click handler invocations.
There are two ways to fix this problem :
1) put after the object that fires twice this two methods
Event.stopPropagation();
Event.preventDefault();
or just replace Event with arg0 if you are using key/mouse etc. handlers. If this doesn't stop from calling it twice go with the second way
2)
declare in the class this variable -> private RegistrationHandler stopHandler;
then when you add the click/mouse/keypress handler write this :
stopHandler=addButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String check = Cookies.getCookie("role");
if(check == "TopBeheerder") {
boolean currentMode = mode0.getValue();
add addObject = new add();
if(currentMode == true) {
addObject.bookMode();
}
if(currentMode == false) {
addObject.userMode();
}
}
else {
BibPhp.notification("You don't have enough permissions.");
}
stopHandler.removeHandler();<-----------------
}
});
put stopHandler.removeHandler()
where do you need to stop from repeating twice.
p.s: if you don't find removeHandler()
method, write stopHandler(.dot)
and then search for the method
My guess is that, somehow, you are adding the ClickHandler twice.
I would do something like this to debug:
System.out.println("adding click handler!");
addButton.addClickHandler(new ClickHandler() { ...
If you see "adding click handler!" print twice, that is your answer.
I ran into a similar problem a while back when implementing an MVP pattern. The View was static and initialized only once, however, the Presenter was initialized each time the Place was requested. Each time the Presenter was initialized it would bind the click event, in turn adding the ClickHandler multiple times.
I had a similar problem with GWT Buttons.
I was creating a Button inside a Class called "Widgets.java", without writing the listener in that class. Then I was using this button as "ClassName.ButtonName", like a macro: "Widgets.myButton".
Of course, at a certain point, I was adding a listener to this referenced button from another class and although I was deleting and cleaning the full RootPanel, when I had to redraw again the button with the same name and so, it seemed that the button KEPT the listener already associated and added A NEW ONE again.
So every time I clicked that button, the function associated to it, ran twice.
Solution? Just stop using a "reference" to a Button and create it locally.
I read this thread and that "enlightened" me to reach my solution, just wanted to make my two cents. :)
Here's how I solved it : I declared an Handler Registration variable like this
private HandlerRegistration HandlerButton;
then when you need to call the addbutton method,use the variable created before
HandlerButton=addButton.addClickHandler(new ClickHandler() {
public void onClick(ClickEvent event) {
String check = Cookies.getCookie("role");
if(check == "TopBeheerder") {
boolean currentMode = mode0.getValue();
add addObject = new add();
if(currentMode == true) {
addObject.bookMode();
}
if(currentMode == false) {
addObject.userMode();
}
}
else {
BibPhp.notification("You don't have enough permissions.");
}
event.stopPropagation();
event.preventDefault();
HandlerButton.removeHandler();
}
});
at the end of the method you just call stopPropagation(),preventDefault() and remove the handler to be sure that it doesn't fires more than one time