Results 1 to 2 of 2
  1. #1
    mwr1976 is offline Senior Member
    Join Date
    Oct 2011
    Posts
    106
    Rep Power
    0

    Default Problem inside of event handler

    I have to String arrays: names and titles. When some one types the name in the names array in nameTxtField I want the corresponding index in the titles array to show in the titleTxtField. When I click the submit button nothing happens. Here is my complete code:

    Java Code:
    import java.awt.*;
    import javax.swing.*;
    import java.awt.event.*;
    
    public class EmployeeTitle extends JApplet
    {
    	int index;
    	
    	JLabel name = new JLabel("name:");
    	JLabel title = new JLabel("title:");
    	JLabel result = new JLabel();
    	
    	JButton submit = new JButton("submit");
    	JTextField nameTxtField = new JTextField();
    	JTextField titleTxtField = new JTextField();
    
    	String[] names = {"Mike Ronfield", "John Setters", "Bob Bobski"};
    	String[] titles = {"Manager", "Cashier", "Supervisor"};
    	
    	public void init()
    	{
    		Container con = getContentPane();
    		con.setLayout(new GridLayout(4,4));
    		con.add(name);
    		con.add(nameTxtField);
    		con.add(title);
    		con.add(titleTxtField);
    		con.add(result);
    		con.add(submit);
    		
    		SubmitButtonHandler  sbHandler;
    		sbHandler = new SubmitButtonHandler();
    		submit.addActionListener(sbHandler);
    		
    		
    		
    	}
    	
    	private class SubmitButtonHandler implements ActionListener
    	{
    		public void actionPerformed(ActionEvent e)
    		{
    			
    			if(nameTxtField.getText().toString() == names[0])
    			{
    				 titleTxtField.setText(titles[0]);
    			}
    				
    			
    			
    		}
    	}
    	
    	
    }
    Thanks in advance

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Problem inside of event handler

    Don't use == to check Strings for equality. Instead use the equals or equalsIgnoreCase method. Even better would be to use a Map such as a HashMap to tie a name to a title. Better still would be to create an object that holds name and title.

Similar Threads

  1. event handler issues
    By kd7vea in forum AWT / Swing
    Replies: 11
    Last Post: 04-22-2011, 12:45 AM
  2. JAX-RPC Registering a Client-Side Handler Problem
    By memzback in forum New To Java
    Replies: 0
    Last Post: 05-12-2010, 06:43 PM
  3. Replies: 0
    Last Post: 03-22-2010, 06:45 AM
  4. event handler not working properly
    By H3rtaherta in forum Java 2D
    Replies: 3
    Last Post: 11-24-2008, 02:39 AM
  5. JList and JButton event handler not working
    By H3rtaherta in forum AWT / Swing
    Replies: 3
    Last Post: 11-22-2008, 12:00 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •