Thanks I used the third option and it worked fine.
On Jul 21, 6:04 pm, Neha Chachra <neha.chac...@gmail.com> wrote:
> Oh, sure. Here's one that should work:
> import <relevant classes like Button, clickhandler, Window>
>
> Button[] myButtons = new Button[2];
> myButtons[0] = new Button("clickMeButton");
> myButtons[1] = new Button("clickMe2");
>
> for(int i=0;i<2;i++){
> myButtons[i].addClickHandler(new ClickHandler(){
> Button sourceButton;
>
> @Override
> public void onClick(ClickEvent event) {
> sourceButton = (Button)event.getSource();
> if(sourceButton.getText().equals("clickMeButton")) {
> Window.alert("works");
> }
> if(sourceButton.getText().equals("clickMe2")){
> Window.alert("works2");
> }
> }
>
> });
>
> If you don't want an additional sourceButton, you can also do:
> ((Button)event.getSource()).getText().equals("clickMeButton"); though as the
> size of your array increases you'll end up making a lot of unnecessary calls
> to getSource( ) that way.
>
> Another approach would be to simply implement the ClickHandler in a separate
> class, say if you have a lot of buttons and a long if/else branching. You
> could do:
>
> public class MyClickHandler implements ClickHander {
> Button sourceButton;
>
> @Override
> public void onClick(ClickEvent event) {
> sourceButton = (Button)event.getSource();
> if(sourceButton.getText().equals("clickMeButton")) {
> Window.alert("works");
> }
> if(sourceButton.getText().equals("clickMe2")){
> Window.alert("works2");
> }
> }
>
> }
>
> MyClickHandler myHandler = new MyClickHandler();
> myButtons[i].addClickHandler(myHandler); // for both your buttons
>
> To be honest, I haven't done much work with ClickHandlers yet, so there
> might be a better approach to handle such scenarios. In particular, I would
> check if assigning and using DOM ids for the widgets is an option. I am sure
> it must be, I just haven't gotten around to that. :)
>
> Either way, I hope this helps.
>
> -neha
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Google Web Toolkit" group.
To post to this group, send email to Google-Web-Toolkit@googlegroups.com
To unsubscribe from this group, send email to
google-web-toolkit+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/Google-Web-Toolkit?hl=en
-~----------~----~----~----~------~----~------~--~---