Either you've made a copy-and-past error in the code you posted here, or there's another 
"symbol" variable in scope because the one declared at the start of your 
addStock() method is not in scope inside methods on your ClickHandler.

To make it work (and I believe the way the tutorial shows it), symbol is declared 
"final", as in:
    final String symbol = ...

By declaring it final, the compiler knows the value will not change, and so it 
can safely take a copy of its value when it constructs the new ClickHandler 
instances, and store that value in an instance field on the ClickHandler. This 
then allows it to be in scope on methods in the ClickHandler.

Paul

On 31/01/11 17:53, Artur wrote:
Hi

I'm new to GWT. At the moment I'm reading the tutorial for the
StockWatcher (http://code.google.com/intl/de-DE/webtoolkit/doc/latest/
tutorial/codeclient.html).
Here is the code I do not understand:

public void addStock(){

   String symbol = txtBox.getText();

   // Add a button to remove this stock from the table.
     Button removeStockButton = new Button("x");
     removeStockButton.addClickHandler(new ClickHandler() {
       public void onClick(ClickEvent event) {
         int removedIndex = stocks.indexOf(symbol);
         stocks.remove(removedIndex);
         stocksFlexTable.removeRow(removedIndex + 1);
       }
     });
     stocksFlexTable.setWidget(row, 3, removeStockButton);

}

At the first line a new Button is created. Afterwards the button gets
a new anonymous ClickHandler with the "onClick()" function. So far so
good.
But when the user clicks the button and the "onClick()" function is
called, how is it possible, that the String variable "symbol" still
lives in memory?
I have implemented this code to my eclipse and it works perfectly. But
I do not understand how is it possible to get the value of the
"symbol" variable after the button has been created and sent to
client.??? When user clicks on the button the "onClick()" function is
called and still has the value of the "symbol" string??'

Can someone explain it to me?

Thanks
Artur


--
You received this message because you are subscribed to the Google Groups "Google 
Web Toolkit" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/google-web-toolkit?hl=en.

Reply via email to