Swing Java Employee Program
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
secondJTextField. Include twoJLabels to describe theJTextFields used for
data entry, and include a third JLabel that holds the employee's title or
an error message if no match is found for the employee
Every thing works fine but when i include following part of code only
error message shown..please help..thanks
if(!(name.equalsIgnoreCase(empName[x]) ||
job.equalsIgnoreCase(jobName[x])))
{
errorortitle.setText("Employee not found");
}
package appletLesson;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.Color.*;
public class JEmployeeTitle extends JApplet implements ActionListener
{
String[] empName = {"James", "Tom", "Steve", "Barack", "John"};
String[] jobName = {"Blunt", "Kites", "Jobs", "Obama", "Smith"};
String[] jobTitle = {"Actor", "Buisness men", "CEO", "President", "Painter"};
JLabel fnameLabel = new JLabel("Enter First Name:");
JLabel lnameLabel = new JLabel("ENter Last Name:");
JButton button = new JButton("Submit");
JLabel errorortitle = new JLabel(" ");
JTextField fnameField = new JTextField(20);
JTextField lnameField = new JTextField(20);
Container con = getContentPane();
public void init()
{
con.setBackground(Color.YELLOW);
con.add(fnameLabel);
con.add(fnameField);
con.add(lnameLabel);
con.add(lnameField);
con.add(button);
con.add(errorortitle);
con.setLayout(new FlowLayout());
fnameField.addActionListener(this);
lnameField.addActionListener(this);
button.addActionListener(this);
}
public void actionPerformed(ActionEvent e)
{
String name = fnameField.getText();
String job = lnameField.getText();
for(int x = 0; x < 5; x++)
{
if(name.equalsIgnoreCase(empName[x]) ||
job.equalsIgnoreCase(jobName[x]))
{
errorortitle.setText(jobTitle[x]);
}
if(!(name.equalsIgnoreCase(empName[x]) ||
job.equalsIgnoreCase(jobName[x])))
{
errorortitle.setText("Employee not found");
}
}
validate();
}
}
No comments:
Post a Comment