Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Basic Applet Not Functioning Correctly

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Basic Applet Not Functioning Correctly

    Okay, so I'm in an Intro to Java class, and we're currently covering applets. This assignment is giving me a hard time, it reads as follows:

    "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.

    Any help on this would be VERY appreciated, I've been trying really hard at it for the past two days and I'm having lots of trouble figuring it out!

    Here is my HTML code:


    <html>
    <object  code = ”JEmployeeTitle2.class” width = 500 height = 500>
    </object>
    </html>


    Here is my Java code:


    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    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)
    	{
    		boolean valid = false;
    		String[] jobTitle = {"Manager", "Cashier", "Sales", "Maintenance", "Designer"};
    		String[] employeeName = {"John", "Paul", "Rick", "Jennifer", "Susan"};
    		while(valid == false)
    		{
    			for(int x = 0; x < jobTitle.length; x++)
    			{
    				if(jobTextField.getText() == jobTitle[x])
    				{
    					valid = true;
    					con.remove(jobLabel);
    					con.remove(employeeLabel);
    					con.remove(jobTextField);
    					con.remove(employeeTextField);
    					con.remove(submit);
    					lastLabel.setText("The " + jobTitle[x] + " position is held by " + employeeName[x]);
    					lastLabel.setFont(font1);
    					con.add(lastLabel);
    				}
    				if(employeeTextField.getText() == employeeName[x]);
    				{
    					valid = true;
    					con.remove(jobLabel);
    					con.remove(employeeLabel);
    					con.remove(jobTextField);
    					con.remove(employeeTextField);
    					con.remove(submit);
    					lastLabel.setText(employeeName[x] + " holds the position of " + jobTitle[x]);
    					con.add(lastLabel);
    				}
    			}
    		}
    		invalidate();
    		validate();
    	}
    }


  2. #2
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Applet Not Functioning Correctly

    Could also be a problem with how I managed my if-statements, really clueless on this one!

  3. #3
    Senile Half-Wit Freaky Chris's Avatar
    Join Date
    Mar 2009
    Posts
    834
    My Mood
    Cynical
    Thanks
    7
    Thanked 105 Times in 90 Posts

    Default Re: Basic Applet Not Functioning Correctly


  4. #4
    Junior Member
    Join Date
    Nov 2011
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Basic Applet Not Functioning Correctly

    Yeah I noticed that and changed it all up, here is the updated code that I have, still producing the same result:

    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;
     
    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 < 5; x++)
    		{
    			if(job.equals(jobTitle[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 " + employeeName[x]);
    				lastLabel.setFont(font1);
    				con.add(lastLabel);
    			}
    			if(name.equals(employeeName[x]));
    			{
    				con.remove(jobLabel);
    				con.remove(employeeLabel);
    				con.remove(jobTextField);
    				con.remove(employeeTextField);
    				con.remove(submit);
    				lastLabel.setText(employeeName[x] + " holds the position of " + jobTitle[x]);
    				con.add(lastLabel);
    			}
    		}
    		invalidate();
    		validate();
    	}
    }

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Basic Applet Not Functioning Correctly

    Call repaint() after calling validate().

    Take a very careful look at this line: if(name.equals(employeeName[x]));{

    If it's not obvious to you, put some print statements inside that if statement. When is it happening?
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Threading not functioning
    By feonx in forum AWT / Java Swing
    Replies: 1
    Last Post: October 31st, 2011, 02:56 PM
  2. Replies: 3
    Last Post: October 19th, 2011, 07:57 AM
  3. Connection pool and Datasource Implementation Is Not Functioning
    By mychickbad in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 14th, 2011, 06:15 PM
  4. I can't get this loop to work correctly
    By Nismoz3255 in forum Loops & Control Statements
    Replies: 1
    Last Post: February 27th, 2011, 04:20 PM
  5. Replies: 3
    Last Post: November 9th, 2010, 01:19 PM

Tags for this Thread