Results 1 to 2 of 2
Thread: Problem inside of event handler
- 11-12-2011, 03:52 AM #1
Senior Member
- Join Date
- Oct 2011
- Posts
- 106
- Rep Power
- 0
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:
Thanks in advanceJava 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]); } } } }
-
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
-
event handler issues
By kd7vea in forum AWT / SwingReplies: 11Last Post: 04-22-2011, 12:45 AM -
JAX-RPC Registering a Client-Side Handler Problem
By memzback in forum New To JavaReplies: 0Last Post: 05-12-2010, 06:43 PM -
creating itemstate event handler of array of combobox at runtime.
By pophils in forum AWT / SwingReplies: 0Last Post: 03-22-2010, 06:45 AM -
event handler not working properly
By H3rtaherta in forum Java 2DReplies: 3Last Post: 11-24-2008, 02:39 AM -
JList and JButton event handler not working
By H3rtaherta in forum AWT / SwingReplies: 3Last Post: 11-22-2008, 12:00 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks