Appending a String within a ClickHandler.

19 views
Skip to first unread message

Gautam

unread,
Mar 15, 2012, 4:53:29 AM3/15/12
to Google Web Toolkit
Hi,
I am relatively new to GWT, I apologize if my doubt sounds juvenile.

I have a requirement where I need to append contents to a String in a
Button's ClickHandler. In the ClickHandler, i do something like this :

String string = "";
for(int i=0; i<Panel.getWidgetCount(); i++)
{
Label l = (Label)Panel.getWidget(i);
string = rb.getText();
}

I do this because the number of labels in the panel is decided at run-
time only. My problem is that i want this "string" to be available
outside the scope of the Button's ClickHandler. If i declare "string"
outside the ClickHandler, i cannot access it because it is a non-final
variable. If i use the keyword final with"string", i cannot modify it
within the ClickHandler.

What can i do here? I apologize once again if this sounds silly but i
couldn't find a solution anywhere.
Any help would be greatly appreciated.

Thanks in advance.

Regards,
Gautam

Patrick Tucker

unread,
Mar 16, 2012, 9:27:53 AM3/16/12
to google-we...@googlegroups.com
You have a few different options:
  1. Use an object like StringBuilder, which stores a string that is modifiable.
  2. Wrap your string in a custom object which can be final, acts like a place holder.
  3. ...
I generally use number 2, unless of coarse I am modifying the string, then a StringBuilder should be used.
 
public class StringPlaceHolder {
   public String theString = null;
}
 
public final StringPlaceHolder placeHolder = new StringPlaceHolder();
 
..your click handler..
  public void onClick(ClickEvent event) {
    ...
    placeHolder.theString = something;
  }
 
This code may not be exactally correct, but should get the point across.
Reply all
Reply to author
Forward
0 new messages