Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Newbie JTextField Listener question

 
Scott Matthews
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I have many JTextFields on the same screen and want to have 1 listener for all of them:
i.e. myJTextField1.addActionListener(this);
myJTextField2.addActionListener(this);
.
.
myJTextField15.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
Thanks.

------------------
 
Paul Stevens
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why are you using ActionEvent for JTextField? Just curious.
 
Nathan Pruett
Bartender
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...

-Nate
 
Thomas Suer
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Scott!
Two possibilities:
1.)
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)
{
// ...
}
// ...
}
2.)
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.

Tom
------------------
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic