Java Reference
In-Depth Information
s = QualifiedCoordinates.convert(c.getLongitude(), r);
cl.append(s);
}
}
} catch(Exception e) {};
if ( cl.length()>0){
location = cl.toString();
} else {
location = "Could not determine location.";
}
getDisplay().callSerially(new LocationUpdater(location));
}
}
The core UI implementation remains the same, but I made a few changes for clarity.
The most obvious change is that I renamed the
Location
,
LocationStore
, and
LocationParser
classes to
WeatherLocation
,
WeatherLocationStore
, and
WeatherLocationParser
, respectively.
This isn't strictly necessary, as I could resolve the naming collision between my use of
Location
as a class name throughout the topic and the Location API's
Location
class using
package qualifiers in the
WeatherWidget
class and elsewhere, but doing so may be confusing.
To support the location features, I added the
locatingAlert
to provide status while
the MIDlet uses the Location API. I also added the
Command
instance
locateCommand
to the
locationList
and
locationTextBox
screens. Using the same scheme for organizing UI code
that NetBeans imposes, I added lazy constructor-fetcher methods for these items. I could
have added them directly using the NetBeans GUI builder, but small changes like this fre-
quently go faster if you edit the code by hand.
■
Caution
Choosing whether to rely on the NetBeans GUI builder or hand-build GUIs (in whole or in part) is
a difficult decision. I find that using NetBeans to lay out my GUI gives me a way to rapidly prototype an appli-
cation's flow, although it's sometimes faster to make small tweaks by hand. For this topic, I chose to use the
GUI builder for initial examples and then hand-edit those examples to minimize the amount of additional
code that gets added to a project (either by me or NetBeans itself). Doing this can be dangerous, because
the NetBeans code generator doesn't like you changing its autogenerated code. In practice, it's best to either
rely on a code-generation tool or build code by hand, and not mix the two approaches.
To show you the location that the MIDlet determines with the Location API, I
tweaked the implementation of the
set_location
method slightly. In addition to updating
the MIDlet's
wxlocation
field, the
set_location
method also sets the
locationTextBox
's
contents to the location the MIDlet has determined with the API.