Java Reference
In-Depth Information
Tutorial: Finishing the Employee Application, Part 2
Because we have put the option to display employee information on the Employee Application Menu, the display
button on EnterEmpInfo isn't needed any more. What we really need is an update/insert button We will change the
display button's name to actionBtn and, depending on the function selected from the menu, set the button's label to
the appropriate text.
1.
Open EnterEmpInfo in the Visual Editor.
2.
Rename dispBtn to actionBtn and change the button text from “Display” to “Insert.”
This will define the default button text as “Insert.” The application must change the button text to “Update” when
the user has selected update as the function. If an Employee object is passed from EmpNumFrame, EnterEmpInfo
knows that the function is update. So, we need to create a new constructor that
Accepts an Employee object
Changes the button text to Update
Retrieves the information from the Employee object
Puts the Employee object's property information into the text fields
3.
Add the following Java statements to start creating the new constructor
public EnterEmpInfo(Employee e) {
this ();
emp = e;
actionBtn.setLabel("Update");
}
That first statement probably looks funny. The first statement executes the class's null constructor. Since all of the
set-up functions still need to be performed, we simply run the null constructor instead of duplicating the code in the
new constructor.
The second statement assigns the employee object (assigned to the method variable e ) to the class variable emp
(so other methods can access the object). And, of course, the last statement changes the button text.
Now we need to modify EmpNumFrame to send the Employee object when update is selected by the user.
4.
In EmpNumFrame's itemStateChanged method, after the current if statement, add
the following:
if (txType.equals("update")) {
EnterEmpInfo eei = new EnterEmpInfo(emp);
}
We should be able to select a particular employee for update, and when the EnterEmpInfo frame is displayed,
the button should have update as the text. Let's test.
5.
Run TNT, select Employee Application, Update Employee Information, and then select
employee number 111.
The EnterEmpInfo frame should be displayed, and the action button text should be Update. None of the
employee information is displayed, because we still must insert the code to retrieve the employee information and
populate the text fields. We will create a separate method called populateTFs to do this and have the constructor
invoke the method.
 
Search WWH ::




Custom Search