"Create a JApplet that contains two parallel arrays that contain at least five employees' names and jobs. Allow the user to enter either a title or a name and to click a JButton to display the other. Include a JLabel to describe each JTextField."
I know the formatting of the JFrame is a bit awkward, but I'll fix that later, my main problem is that when someone enters something in the job title box and presses submit, it should give the name of the person with that job title, and vice versa. However, no matter what valid job title or name I enter into either JTextField, it always ends up reading "The Designer position is held by Susan" along with not removing the components that I want removed until it is minimized and brought back up. So it displays the wrong message, along with the removal problem.
Here is my html code:
<html> <object code = ”JEmployeeTitle2.class” width = 500 height = 500> </object> </html>
And here is my Java code:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class JEmployeeTitle2 extends JApplet implements ActionListener
{
JLabel jobLabel = new JLabel("Enter a job title below.");
JLabel employeeLabel = new JLabel("Enter an employee name below.");
Font font1 = new Font("Times New Roman", Font.BOLD, 20);
JTextField jobTextField = new JTextField(20);
JTextField employeeTextField = new JTextField(20);
JButton submit = new JButton("Submit");
JLabel lastLabel = new JLabel(" ");
Container con = getContentPane();
public void init()
{
jobLabel.setFont(font1);
employeeLabel.setFont(font1);
con.add(jobLabel);
con.add(employeeLabel);
con.add(jobTextField);
con.add(employeeTextField);
con.add(submit);
con.setLayout(new FlowLayout());
submit.addActionListener(this);
jobTextField.addActionListener(this);
employeeTextField.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};
String job = jobTextField.getText();
String name = employeeTextField.getText();
for(int x = 0; x < jobTitle.length; ++x)
{
if(job == jobTitle[x])
{
name = employeeName[x];
con.remove(jobLabel);
con.remove(employeeLabel);
con.remove(jobTextField);
con.remove(employeeTextField);
con.remove(submit);
lastLabel.setText("The " + jobTitle[x] + " position is held by " + name);
lastLabel.setFont(font1);
con.add(lastLabel);
}
else
{
for(int y = 0; y < employeeName.length; ++y)
{
if(name == employeeName[y]);
{
job = jobTitle[y];
con.remove(jobLabel);
con.remove(employeeLabel);
con.remove(jobTextField);
con.remove(employeeTextField);
con.remove(submit);
invalidate();
validate();
lastLabel.setText("name " + "holds the position of " + job);
con.add(lastLabel);
}
}
}
invalidate();
validate();
}
}
}
All help here is MUCH appreciated! Trying hard to figure this one out. I'll attach a screenshot of the box I end up with for a better idea.

New Topic/Question
Reply



MultiQuote






|