• Post Reply Bookmark Topic Watch Topic
  • New Topic

Proper use of getText() method

 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suppose I've got a bunch of JButtons, labeled "A"-"G". I want to set up an ActionListener for these buttons, and I want to display the button's text in a text field (call it "field" ) if the button's clicked. The thing that occurred to me to do was this:
public void actionPerformed( ActionEvent evt )
{
Object button = evt.getSource();
String s = button.getText();
field.setText( s );
}
Unfortunately, when I do this I get the error that javac can't resolve the symbol getText(). And this confuses me. I know that if I wrote
if ( button == alpha )
{
field.setText( alpha.getText() )
}
(where alpha is the button labeled "A" ), all would be honky-dory. And the equivalence here suggests that the button object would have the same value as alpha. And if that's so, why can't I just get alpha's text by writing "button.getText()"?
I'm making some basic mistake, I'm sure, but I don't know what it is. What I don't want to have to do is to write separate ActionListeners for each button, knowing that I want to do the same thing--i.e. get the button's text--regardless of the button clicked. Surely there is a method for doing this?
Any casting of light on my ignorance here would be gratefully appreciated!
 
Matt Senecal
Ranch Hand
Posts: 255
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In actionPerformed, change this line:
Object button = evt.getSource();
so that it is:
JButton button = (JButton) evt.getSource();
As you code stands, your "button" is not a button, but a generic Object, and you need to cast it to the proper class.
Originally posted by Adam Vinueza:
Suppose I've got a bunch of JButtons, labeled "A"-"G". I want to set up an ActionListener for these buttons, and I want to display the button's text in a text field (call it "field" ) if the button's clicked. The thing that occurred to me to do was this:
public void actionPerformed( ActionEvent evt )
{
Object button = evt.getSource();
String s = button.getText();
field.setText( s );
}
Unfortunately, when I do this I get the error that javac can't resolve the symbol getText(). And this confuses me. I know that if I wrote
if ( button == alpha )
{
field.setText( alpha.getText() )
}
(where alpha is the button labeled "A" ), all would be honky-dory. And the equivalence here suggests that the button object would have the same value as alpha. And if that's so, why can't I just get alpha's text by writing "button.getText()"?
I'm making some basic mistake, I'm sure, but I don't know what it is. What I don't want to have to do is to write separate ActionListeners for each button, knowing that I want to do the same thing--i.e. get the button's text--regardless of the button clicked. Surely there is a method for doing this?
Any casting of light on my ignorance here would be gratefully appreciated!

 
Adam Vinueza
Ranch Hand
Posts: 76
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, Matt!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!