Google Code offered in: English - Español - 日本語 - 한국어 - Português - Pусский - 中文(简体) - 中文(繁體)
At this point, you've finished implementing the StockWatcher UI and all its client-side functionality. However, you've noticed that there is an error in the Change field. The percentage of change is not calculating correctly.
In this section, you'll use Eclipse to debug your Java code while running StockWatcher in hosted mode.
You can debug the Java source code before you compile it into JavaScript. This GWT develpment process help you take advantage of the debugging tools in your Java IDE. You can:
One of attractions of developing in JavaScript is that you can make changes and see them immediately by refreshing the browser—without having to do a slow compilation step. GWT hosted mode provides the exact same development cycle. You do not have to recompile for every change you make; that's the whole point of hosted mode. Just click "Refresh" to see your updated Java code in action.
Looking at the values in the Price and Change fields, you can see that, for some reason, all of the change percentages are only 1/10 the size of the correct values.
The values for the Change field are loaded by the updateTable(StockPrice) method.
/**
* Update a single row in the stock table.
*
* @param price Stock data for a single row.
*/
private void updateTable(StockPrice price) {
// Make sure the stock is still in the stock table.
if (!stocks.contains(price.getSymbol())) {
return;
}
int row = stocks.indexOf(price.getSymbol()) + 1;
// Format the data in the Price and Change fields.
String priceText = NumberFormat.getFormat("#,##0.00").format(
price.getPrice());
NumberFormat changeFormat = NumberFormat.getFormat("+#,##0.00;-#,##0.00");
String changeText = changeFormat.format(price.getChange());
String changePercentText = changeFormat.format(price.getChangePercent());
// Populate the Price and Change fields with new data.
stocksFlexTable.setText(row, 1, priceText);
stocksFlexTable.setText(row, 2, changeText + " (" + changePercentText
+ "%)");
}
Just glancing at the code, you can see that the value of the changePercentText variable is being set elsewhere, in price.getChangePercent. So, first set a breakpoint on that line and then drill down to determine where the error in calculating the change percentage is.
String changePercentText = changeFormat.format(price.getChangePercent());
stocksFlexTable.setText(row, 1, priceText);

Now step into the code to see where and how the changePercentText is being calculated.
public double getChangePercent() {
return 10.0 * this.change / this.price;
}Looking at the getChangePercent method, you can see the problem: it's multiplying the change percentage by 10 instead of 100. That corresponds exactly with the output you saw before: all of the change percentages were only 1/10 the size of the correct values.
public double getChangePercent() {
return 100.0 * this.change / this.price;
}At this point when you enter a stock code, the calculation of the Change field should be accurate. Try it and see.
At this point you've implemented all your functional requirements. StockWatcher is running and you've found and fixed a bug.
Now you're ready to enhance StockWatcher's visual design. You'll apply CSS style rules to the GWT widgets and add a static element (a logo) to the page.