Create an interface that contains two JTextFields, a JButton, and three JLabels. When the user types an employee’s first and last names (separated by a space) in a JTextField, the employee’s job title is displayed in a second JTextField. Include two JLabels to describe the JTextFields used for data entry. Also include a third JLabel that displays the employee’s title or an error message if no match is found for the employee. Use parallel arrays to store the employees’ names and job titles.
Loading

Tahir AnsariRank #366 · 6.8K reputationPosted Sep 10, 2023, 3:44 AM EDT
import javax.swing.*; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; public class EmployeeInfoInterface extends JFrame { private JTextField firstNameField, lastNameField, jobTitleField; private JLabel firstNameLabel, lastNameLabel, resultLabel; private JButton searchButton; // Parallel arrays to store employee names and job titles private String[] employeeNames = {"John Doe", "Jane Smith", "Bob Johnson"}; private String[] employeeJobTitles = {"Manager", "Engineer", "Sales Associate"}; public EmployeeInfoInterface() { setTitle("Employee Information"); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); setLayout(new GridLayout(4, 2)); firstNameLabel = new JLabel("First Name:"); lastNameLabel = new JLabel("Last Name:"); resultLabel = new JLabel(""); firstNameField = new JTextField(); lastNameField = new JTextField(); jobTitleField = new JTextField(); jobTitleField.setEditable(false); // Make jobTitleField read-only searchButton = new JButton("Search"); searchButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { // Get the entered first and last names String firstName = firstNameField.getText(); String lastName = lastNameField.getText(); String fullName = firstName + " " + lastName; // Search for the employee's job title String jobTitle = findEmployeeJobTitle(fullName); // Update the jobTitleField and resultLabel jobTitleField.setText(jobTitle); if (jobTitle != null) { resultLabel.setText("Employee's Job Title:"); } else { resultLabel.setText("Employee not found"); } } }); // Add components to the frame add(firstNameLabel); add(firstNameField); add(lastNameLabel); add(lastNameField); add(searchButton); add(resultLabel); add(jobTitleField); pack(); setLocationRelativeTo(null); // Center the frame } // Function to find an employee's job title based on their full name private String findEmployeeJobTitle(String fullName) { for (int i = 0; i < employeeNames.length; i++) { if (employeeNames[i].equalsIgnoreCase(fullName)) { return employeeJobTitles[i]; } } return null; // Employee not found } public static void main(String[] args) { SwingUtilities.invokeLater(new Runnable() { @Override public void run() { new EmployeeInfoInterface().setVisible(true); } }); } }Amit MohantyRank #112 · 20K reputationPosted Sep 11, 2023, 2:14 AM EDT
Mashudu Tshabeni: Before asking for help, it's important to share what steps you've taken so far and where you've encountered difficulties. We're here to assist you, but please begin by telling us what you've done and where you need assistance.