C
Chad M
"Madhur Ahuja" <[email protected]> said:What if I want to perform additional operations on e, for example a
different operation if it is
a button, something different if it is a JButton.
For Example
This is what I am doing:
if(obj.getSource()==x[j]&&obj.getIcon()==null)
Here , I cant replace obj with e, since e doesnt have getIcon.
Doing :
JButton obj=(JButton)e;
But e was the ActionEvent, so of course this won't work. Once you have
determined that e.getSource() is a JButton, you can then cast the result
of getSource() to JButton--you can't cast e itself.
However, in your case you are testing with equality for a reference that
you already have. So if "myJButton" is your existing reference, just do
if(e.getSource()==myButton)
{
// do stuff with myButton, which is already of type JButton.
// no cast needed.
}
However, at this point you really should look at the idea of using
distinct ActionListeners for distinct actions. It makes a lot of the
hoops you are trying to jump through unnecessary.
-Chad