tags:

views:

30

answers:

2

My gwt project have flexTable show data of image and button on each row and coll.
But my button won't work properly. this is my current code:

private Button[] b = new Button[]{new Button("a"),...,new Button("j")};
private int z=0;
...
public void UpdateTabelGallery(JsArray str){
    for(int i=0; i str.length(); i++){
    b[i].setText(str.gettitle());                               
    UpdateTabelGallery(str.get(i));                   
  }
}

public void UpdateTabelGallery(GalleryData str){
  Image img = new Image();  
  img.setUrl(str.getthumburl());  

  HTML himage= new HTML("a href="+str.geturl()+">"+ img +"/a>" + b[z] );                                                        
  TabelGaleri.setWidget(y, x, himage);

//is here th right place?
  b[z].addClickHandler(new ClickHandler(){
  @Override
    public void onClick(ClickEvent event) {
      Window.alert("I wan to show the clicked button text" + b[z].getText());               
    }                         
  });

  z++;
}



I'm still confuse where I should put my button handler. With this current code seems the clickhandler didn't work inside a looping. And if I put it outside loop its not working because I need to know which button clicked. I need to get my index button.but how? Is there any option than array button?

thanks

A: 

The problem is that you refer to 'z' in your click handler, but the value of z changes, so that when your click handler is actually called the value of z is wrong.

You need a local final variable in UpdateTabelGallery which you assign the current value of z to to allow it to be captured by the handler you create. Even better, get rid of z entirely and pass i to UpdateTableGallery:

public void updateTableGallery(GalleryData str, final int i){
  Image img = new Image();  
  img.setUrl(str.getthumburl());  

  HTML himage= new HTML("a href="+str.geturl()+">"+ img +"/a>" + b[i] );                                                        
  TabelGaleri.setWidget(y, x, himage);

//is here th right place?
  b[i].addClickHandler(new ClickHandler(){
  @Override
    public void onClick(ClickEvent event) {
      Window.alert("I wan't to show the clicked button text" + b[i].getText());               
    }                         
  });

}

But what do you expect:

  HTML himage= new HTML("a href="+str.geturl()+">"+ img +"/a>" + b[i] );       

to do? Aside from the incorrect HTML syntax, I don't think adding ypur button to the string will work.

tdavies
wow, thanks for the sample code. you're right. the button didn't working if i put it in HTML.
A: 

Did you test this above sample?

bhathiya
yeah, its working!