File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Swing / AWT / SWT and the fly likes Newbie JTextField Listener question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Newbie JTextField Listener question" Watch "Newbie JTextField Listener question" New topic

Newbie JTextField Listener question

Scott Matthews
Ranch Hand

Joined: Jun 28, 2001
Posts: 36
If I have many JTextFields on the same screen and want to have 1 listener for all of them:
i.e. myJTextField1.addActionListener(this);
How can I tell which JTextField caused the action event when I arrive at "actionPerformed":
public void actionPerformed(ActionEvent e) {
I have tried the following in this "actionPerformed":
JTextField source = (JTextField)(e.getSource());
String strTextFieldName = source.getName();
The above returns "null" for strTextFieldName
I was hoping that strTextField name would return "myJTextField1 or myJTextField2, etc.
What do I need to do?

Related question: When I have a screen of JLabels and JTextFields, is there normally different listener methods set up on for each JTextField or just 1 listener method with a bunch of "if" statements to tell which JTextField caused the action

Paul Stevens
Ranch Hand

Joined: May 17, 2001
Posts: 2823
Why are you using ActionEvent for JTextField? Just curious.
Nathan Pruett

Joined: Oct 18, 2000
Posts: 4121

If you want to use getName() on your text fields, you're going to have to set their names with setName( String ) first...

That's actually a pretty good way to differentiate between components in a listener. The alternative is to make your components class level attributes and have a test like this:

I personally like the setName()...getName() technique better, because your class is less cluttered with GUI attributes...

Having one listener or multiple listeners is basically a personal preference, I have seen it done both ways... There may be memory issues involved ( i.e. one listener class takes up less memory than several ), but that's a little over my head... Perhaps someone else who knows a little bit more about performance can give you a definite answer on this... Though, personally, I don't think it matters a whole lot if you use one listener or multiple listeners...


Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Thomas Suer
Ranch Hand

Joined: Sep 03, 2001
Posts: 50
Hi Scott!
Two possibilities:
Assuming that your JButtons are constructed like this:
JButton myJTextField1 = new JButton("Button #1");
Then in the actionPerformed(ActionEvent evt) method do the following:
public void actionPerformed(ActionEvent e)
// ...
if (evt.getSource() == myJTextField1)
// do what you want...
else if (evt.getSource() == myJTextField2)
// ...
// ...
Or, if you want to go your way, just invoke the setName() method of the buttons and give them the names you want them to have. You have to know that initially the buttons names are 'null'!
To your question regarding the number of listeners:
it depends on what you want to do. You could use one listener for all JTextFields and implement one if...else statement, or you could use an own listener for each JTextField. I think there is no rule that says that you have to do the one or the other.

I agree. Here's the link:
subject: Newbie JTextField Listener question
It's not a secret anymore!