Forums Register Login

Newbie JTextField Listener question

+Pie Number of slices to send: Send
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.

------------------
+Pie Number of slices to send: Send
Why are you using ActionEvent for JTextField? Just curious.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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
------------------
I claim this furniture in the name of The Ottoman Empire! You can keep this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1448 times.
Similar Threads
setText() not working
Can I use repaint() on a JFrame?
Can I use repaint() on a JFrame?
Understanding Events
ActonListener
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 15:36:01.