LAB ACTIVITY 3L
EVENT HANDLING
EVENT HANDLING
Learning Outcomes
By the end of this lab, students should be able to :
Write Java programs using Event Handling
Write Java programs using Event Handling with GUI Components
Hardware/ Software : Personal Computer, Java Development Kit version X.x.x., NetBeans IDE 12.0
LAB ACTIVITY 3L
Activity Outcome: Student know how to create event handling program for text field.
Using the coding from Lab Activity 2A, we will integrate event handling functionality from that activity into this project. Name new Lab Activity as LabActivity3K.
Copy the code into a new activity and ensure it is copied correctly. Fix any errors if present. Run the program and make sure the interface matches the diagram shown below.
In this lab Activity, we will
Create Action Listeners for Buttons:
To handle the events for your buttons, we will need to implement ActionListener.
For each button (Calculate, Clear, Display), we need to define what actions should occur when they are clicked.
Implementing ID Validation (Number Only):
For the ID field, ensure that the user can only input numbers. You can do this by validating the input inside the action listener of the Calculate button. You can use try-catch to handle non-numeric input errors.
Implementing Name Validation (Letters Only):
For the Name field, ensure that the user inputs only letters. This can be done by checking if the input contains non-alphabetic characters, and showing an error message if invalid input is detected.
Set Basic Salary Based on Department:
In the ActionListener for the JComboBox (which represents the department selection), you'll need to automatically fill in the basic salary when a department is selected.
You can set predefined salaries for each department and update the text field accordingly.
Handle Overtime Hours Input via JOptionPane:
When the user clicks on the Overtime Hour field, a dialog box (JOptionPane) should prompt the user to enter the number of overtime hours.
The total overtime salary should then be calculated as RM 150 per hour.
6. Calculate Total Salary: When the Calculate button is clicked, you need to compute the total salary using the formula:
Total Salary = Basic Salary + Total Elaun + Total Overtime - Total Deduction
7. After calculating, update the Total Salary text field with the result.
8. Clear All Fields: When the Clear button is clicked, all text fields should be cleared. Ensure that you reset all the values, including the department selection and radio buttons.
9. Display Information: When the Display button is clicked, the information entered in all the fields (ID, Name, Gender, Department, Basic Salary, Total Elaun, Overtime Hours, Total Overtime, Total Deduction, Total Salary) should be displayed in a JTextArea or in a similar output area.
1. Add ActionListener Interface:
In the LabActivity2A class, add implements ActionListener to allow buttons to respond to clicks.
2. Attach Action Listeners to Buttons:
Inside the constructor (LabActivity2A()), attach the action listeners to your buttons.
3. Define the actionPerformed Method:
Implement the actionPerformed method to handle the events.
4. Clear Fields Method:
You can create a method to clear all the fields when the Clear button is clicked.
5. Display Information Method:
Create a method to display all the entered information.
6. Set Basic Salary Based on Department:
Add an ActionListener for the department ComboBox (cJabatan) to auto-fill the basic salary.
7. Overtime Hours Input with JOptionPane:
When the user clicks on the Overtime Hour field, prompt them to enter the hours via a dialog box.
9. Observe Your Output