CS 209 Spring, 2006 Lab 8: GUI Development Instructor: J.G. Neal Objectives: To gain experience with the programming of: 1. Graphical user interfaces (GUIs), 2. GUI components, and 3. Event handling (required for GUI programs). Notes: 1. This lab exercise correlates with Chapters 12 and 13 of the textbook. The most important sections are: 12.1-12.3 and 13.6-13.9. 2. You need to understand what an event is and when an event occurs. You need to understand what is meant by event handling. 3. In the Java code of a program, be sure that you know how to register an event listener object with the GUI control for which it is to await (listen for) events. You should be able to write the code to have a listener method react/respond to an event generated by its associated GUI component. 4. In this exercise, we are dealing with two types of events: a. ActionEvent: Generated by hitting the Enter key in a JTextField. So an ActionListener object must be registered with the JTextFields. The method addactionlistener is used to register a listener object with a JTextField. b. ItemEvent: Generated by selecting an item from a JComboBox (drop list). So an ItemListener object must be registered with the JComboBoxes. The method additemlistener is used to register a listener object with a JComboBox. Lab Instructions: 1. Obtain the Java files, named EmployeeAryApplicJF.java and EmployeeAryApplicJFMain.java, from instructor s webpage. 2. Create a NetBeans project for this assignment. Add the instructor-provided files to the project. The Java class/file EmployeeAryApplicJFMain.java must be used as the main class/file. The file EmployeeAryApplicJF.java implements a JFrame window. Add your employee classes to the project. That is, add your Employee, HourlyEmployee, SalariedEmployee, and UnionEmployee classes to the project. Be sure that your employee classes fulfill the requirements (meet the specifications) of the previous assignments. 3. Add required constructor to your existing classes. To run the initial program provided by your instructor, each of your four employee classes must have a constructor that takes two parameters: String nam, int idnumb. If your classes do not have such a constructor, add the constructor to each (do not delete any of the constructors that you already have in the classes, however). 4. Compile and run the program. After entering some employees, the JFrame window looks like the window shown below. The data displayed for each employee should include all the member variables, even though they are not yet being set by the current version of the program. (You will add capabilities to do this.) The displayed information on each subclass employee must be generated by the inherited tostring method of the parent Employee class. Page 1 of 7
5. Extend the program. Now you will modify and extend the program as described in the following steps. 6. Mod 1. Add components (controls) to the GUI. Change the GridLayout to accommodate the additional GUI components that you will add in the steps below. In the modifications described in the following steps, all JLabels must be in the left-side column of the GridLayout, and the JTextFields and JComboBoxes must be in the right-side column. Enable user entry of employee s job title: o Add another JLabel to prompt for the employee s job title. o Add another JComboBox to enable the user to select the job title for the employee. o At the top of the main class, create an array of job title names that consists of the following: Administrative Assistant, Clerk, Engineer, Manager, Quality Engineer, Salesperson, Secretary, Service Specialist, Software Engineer, and Technician. This array is similar to the array of employee types, which is declared as a variable at the top of the main class. Both are arrays of Strings. o You need to declare an additional variable at the top of the main class to hold the value selected by the user via the new JComboBox (just like for the employee type when the user selects employee type from the corresponding JComboBox). For this variable, use the name selectedjobtitle. This additional variable is declared so as to have class scope since it must be referenced/used in the listener class and in the methods of the EmployeeAryApplicJF class. Page 2 of 7
Enable user entry of employee s pay rate: o Add another JLabel to prompt for the employee s pay rate. o Add another JTextField to enable the user to input the employee s pay rate. Enable user entry of employee s hours worked: o Add another JLabel to prompt for the employee s hours worked. o Add another JTextField to enable the user to input the employee s hours worked. 7. Mod 2. Wire up your new GUI components (controls) to event handlers. Register an event listener (handler) with each of the new GUI controls so that when the user triggers an event via the use of a control, the event handler will react to the event and cause the correct action to be taken by the program. This applies to the new additional JTextFields and the new additional JComboBox. The next bullet items cover this further. This means that if the user hits the Enter key in any of the newly added JTextFields, this should cause the new employee to be added to the array and to have the entire updated list of employees displayed in the JTextArea, just like hitting the Enter key in one of the existing JTextFields. You will need to modify and extend the method actionperformed in the MyActionListener class. When the user selects from the new JComboBox, this should trigger the updating of the contents of the job title variable (selectedjobtitle) declared at the top of the class. You will need to modify and extend the method itemstatechanged method in the MyItemListener class to accomplish the above. Be sure that the contents of all the JTextFields are cleared when the user hits the Enter key to trigger creation of the new additional employee. 8. Mod 3. In the addemployeetoarray method, modify the creation of employee instances. Depending on the type of employee selected by the user via the JComboBox (drop list), the program must create an instance of the correct employee subclass (HourlyEmployee, SalariedEmployee, or UnionEmployee), as it currently does. Your program must not create any instances of the top level Employee class. You must add parameters to the formal parameter list of the addemployeetoarray method. You must add parameters for: job title, pay rate, hours worked. When creating the employee instance, the code of this method must call the constructor in the correct subclass and pass the following parameters: name, id number, job title, pay rate, and hours worked. (You need to add this constructor in each class.) Do not pass employee type as a parameter. It is not necessary. Each class should initialize the employeetype variable without passing a parameter. Each class should know its type without passing a parameter. 9. Mod 4. Modify the call to the addemployeetoarray method. In the call to the addemployeetoarray method, add the actual parameters for job title, pay rate, hours worked. The total parameter list must consist of only the following items: name, id number, job title, pay rate, and hours worked. Page 3 of 7
10. Mod 4. Modify your employee classes. In each of your employee classes, add a constructor that has a parameter list that consists of a parameter for each of the following, and only the following: name, id number, job title, pay rate, and hours worked (as mentioned above). The constructor in the correct class with the above-mentioned parameter list must be called whenever an employee instance is created in your program. As stated in the above numbered step, do not pass employee type as a parameter. It is not necessary. Each class should initialize the employeetype variable without passing a parameter. Each class should know its type without passing a parameter Modify your employee classes so that the code is optimal. This means, for example, that you only add jobtitle as a protected variable in the parent Employee class and have the subclasses inherit this member variable. You should not add the job title as a member variable in any of the employee subclasses. No member variables should be declared in any of the subclasses. To clarify: the HourlyEmployee, SalariedEmployee, and UnionEmployee classes must only contain constructors and a computepay method, and nothing else. These three subclasses must not have a tostring method. You must have a tostring method in the top level Employee class, but not in any of the subclasses (not in the HourlyEmployee, SalariedEmployee, or UnionEmployee classes). Your Employee class must have a tostring method, but be sure that none of the other employee classes have a tostring method. Make sure that the tostring method returns a string that includes all of the following information items: employee s name, id number, employee type, job title, pay rate, hours worked, and computed pay. Be sure that the computepay method is called by the tostring method. The tostring method must not contain any math expressions that calculate pay. Furthermore, none of the classes should have logic that selects a compute pay method or formula based on whether hourly, salaried, or union. The employee type should not be passed as parameter to any of the constructors or methods in any of the employee classes or subclasses. Also, none of the classes or methods should have logic that selects an employee type or type name based on whether hourly, salaried, or union (or any representation of these such as ints or chars). Make sure that in the tostring method, all monetary amounts are formatted with a dollar sign and two digits to the right of the decimal point (as in previous exercises). Make sure that in the tostring method, the employee s hours worked value is formatted with two digits to the right of the decimal point (as in previous exercises). 11. Mod 5. Modify the code for the first JLabel for No. of Employees. Modify the code for this JLabel so that the maximum number of allowed employees is displayed in place of the question marks. The code must use the variable name for the max size of the array, and not the actual literal number. The use of the variable name is for maintainability. The literal should be in one place only. 12. Mod 6. Modify the Titled Border and contents of the JTextArea. Page 4 of 7
Change the text of the TitledBorder of the JTextArea to read XYZ Corporation Employees. Eliminate the line of text XYZ CORPORATION EMPLOYEES: from the text displayed within the JTextArea. The contents of the JTextArea should just be the list of employees with all attributes displayed for each employee. For each employee, display the employee s name, id number, employee type, job title, pay rate, hours worked, and computed pay. Make sure that all monetary amounts are formatted with a dollar sign and two digits to the right of the decimal point (as in previous exercises). This is done by the tostring method (see Item 10 above). Make sure that the employee s hours worked is formatted with two digits to the right of the decimal point (as in previous exercises). This is done by the tostring method (see Item 10 above). 13. Compile, execute, and debug the program. Test your program using a sufficient variety of data to make sure that it works on all cases. 14. Run the program and make window captures displaying all the capabilities of your program. See the window captures below for example program input/output displays. 15. Create batch file. Create a batch file as you did for the previous assignments so that your program can be executed without the use of NetBeans. Be sure that the batch file is within your top level NetBeans project folder. 16. Create ReadMe file. Using Microsoft Word, create a file named ReadMe. In this document, insert your name at the top, and on the next line insert the assignment number (e.g., Exercise #8 ). Then enter any comments regarding the assignment. Your comments might include any difficulties encountered, suggestions for improvement of the assignment, etc. Then insert at least two window captures of your JFrame window showing the inputs and outputs from the execution of the program. o The first window capture should be captured when you have entered data on the 4 th employee; it should show the data in the JTextFields and selected items in the JComboBoxes. o The second window capture should show the updated list of employees in the JTextArea that is the result of hitting the Enter key in one of the JTextFields for this new 4 th employee. If you did the extra credit part of the assignment, be sure to state this in your ReadMe file. (The extra credit portion of the exercise is specified below.) Also, include additional window captures that demonstrate the execution of the extra credit version of your program and its extra capabilities. Be sure the ReadMe document file is in your top level project folder. 17. Zip the project folder and all its contents. Create a zip file that contains the entire contents of your NetBeans project folder, including all sub-folders and files. Page 5 of 7
Your project folder should contain your ReadMe file and batch file, as for previous project assignments. Change the name the zip file so that its name consists of your name along with the assignment number, as follows: JohnJones-08.zip. Do not use spaces in the name of the file, use hyphens instead. 18. Extra Credit: In the main class for your project, in the constructor, add a JScrollPane to the GUI to hold the JTextArea. In order to accomplish this, do the following: Create an instance of the JScrollPane class. When invoking the JScrollPane constructor, pass the JTextArea as a parameter to the constructor so that the JTextArea is the component on the JScrollPane (the JScrollPane holds the JTextArea as its content). Add the JScrollPane to the JPanel instead of adding the JTextArea to the JPanel. The above addition of the JScrollPane should provide vertical scroll bars automatically when needed. For example, do not resize the main JFrame window. Then, after entering 4 employees, you will need to scroll vertically to see the data on some of the employees, since the JTextArea is only large enough to display the data on 4 employees. If you need more information, consult the Java 5.0 Documentation on the Java 2 Platform API Specification. TO RECEIVE CREDIT FOR YOUR ASSIGNMENT: Submit the following for credit to Angel Drop Box for Exercise 08: a. The zip file containing your entire NetBeans project folder with all its subfolders and files. b. You must have a batch file to run your program without using NetBeans, and the batch file must be in your top level project folder. c. You must have a ReadMe file that contains your name at the top, the lab number, any comments regarding the assignment, and window captures to show the program execution. The ReadMe file must be in the top level project folder. d. You must submit your zip file to the Lab08 Drop Box for this CS 209 course in Angel (http://angel.ecc.edu). DUE DATE: Tuesday, March 28. EXAMPLE WINDOW CAPTURES FOR THE COMPLETED PROGRAM: See next page. The windows show the extra credit JScrollPane also. Page 6 of 7
Page 7 of 7