Java Reference
In-Depth Information
Figure 3-24.
4.
Replace “New Text” in the empNameLbl.setText statement with emp.getEmpName().
The statement should look like the following:
empNameLbl.setText(emp.getEmpName());
Remember, getters return property values. These values can be used just like a variable. So, instead of “New Text”,
“Mary Worker” (which is returned by the getEmpName method) is used in the setText method. Notice that the methods
are executed from “inside out.” This means when one statement is embedded inside another, the embedded statement
is executed first. In other words, the returned value of the embedded method is used as input to the outer method.
5.
Save the code and run EmployeeApp.
Verify that “Mary Worker” appears in the name label.
6.
Change the empStreetLbl.setText statement to retrieve and display the employee's street address.
7.
Save the code and run EmployeeApp.
Verify that “1 Main St.” appears in the street label.
Now the tricky part: we need to retrieve three pieces of information, format the data (i.e., between city and state
we need to add a comma, between state and zip we need to insert several spaces), and then place the final string into
empCSZLbl. There are many ways to do this, but we will do it all in one statement.
8.
Change the empCSZLbl.setText statement so that the following replaces “New Text”:
emp.getEmpCity() + ", " + emp.getEmpState() + " " + emp.getEmpZip()
Notice that the three getters for the employee city, state and zip are embedded in the setText method. The tricky part
is how they are linked together. A plus sign between String variables acts as a concatenation function. Concatenation
between two (or more) strings results in one string. The resulting string is comprised of the first string followed by the
second string. For instance, the following concatenation, “ABC” + “DEF”, results in a single string of “ABCDEF.”
In our example, we are concatenating five strings: the three strings returned by the getters and the two fixed
constant strings “, ” (comma and a space) and “ ” (four spaces).
Search WWH ::




Custom Search