actionPerformed Problems

I have 2 JTextField objects in the same class that have an action listener associated with them. I was wondering how I can implement my actionPerformed() method so it knows which object generated the action. I have attached the relevent code, commenting out the second actionPeformed() method to show what I am trying to do.

privateChatTextField =new JTextField(27);

privateChatTextField.addActionListener(this);

publicChatTextField =new JTextField(27);

publicChatTextField.addActionListener(this);

publicvoid actionPerformed(ActionEvent evt){

String text = privateChatTextField.getText();

sendToServer("ChatOpponent" + text);

privateChatTextField.selectAll();

}

/*

public void actionPerformed(ActionEvent event) {

String text = publicChatTextField.getText();

sendToServer("ChatAll" + text);

publicChatTextField.selectAll();

}

*/

[1242 byte] By [JavaUser1975a] at [2007-10-1 15:34:17]
# 1

> I have 2 JTextField objects in the same class that

> have an action listener associated with them. I was

> wondering how I can implement my actionPerformed()

> method so it knows which object generated the action.

public void actionPerformed(ActionEvent evt) {

Object src = evt.getSource();

if( src == privateChatTextField ) {

....

}

else if( src == publicChatTextField ) {

....

}

}

Or, as an alternative, use separate ActionListeners (one for each text field).

Torgila at 2007-7-10 19:51:45 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 2
Thanks for wasting my time answering you, after discovering that you had crossposted this at: http://forum.java.sun.com/thread.jspa?threadID=633659Dweeb.
warnerjaa at 2007-7-10 19:51:45 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...
# 3
Doh!
Torgila at 2007-7-10 19:51:45 > top of Java-index,Archived Forums,Java 2 Software Development Kit (J2SE SDK)...