Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Newbie JTextField Listener question

 
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

------------------
 
Ranch Hand
Posts: 2823
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why are you using ActionEvent for JTextField? Just curious.
 
Bartender
Posts: 4121
IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
------------------
 
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic