Is there a reason why getText
causes an error: cannot find symbol
inside the action listener shown in the code? Also if there is, how would I fix this
There is a nice and simple trick you can use...
@Override
public void actionPerformed(ActionEvent e)
{
String butSrcTxt = e.getActionCommand();
}
If you do not specify the actionCommand
for a button, then the text
of the button is used instead.
Now, if you do specify the actionCommand
property for the button AND you still want to know the text (which seems weird to me) you could use something more like...
@Override
public void actionPerformed(ActionEvent e)
{
Object source = e.getSource();
if (source instanceof JButton) {
JButton btn = (JButton)source;
String butSrcTxt = btn.getText();
}
}