Does anyone know why the argument of setActionCommand (for example in
JButton) is a String rather than an Object?
Since the only thing that I ever see done with the value of actionCommand is
to compare its reference it could be any kind of Object. Furthermore if it
could be any kind of Object it could be an Integer, in which case I could
use a switch statement to interpret its value rather than if-then-else.
I know that this interface is not going to change, I am just curious as to
why I have to program in a non-obvious way.

Signature
Jim Cobban jcobban@magma.ca
34 Palomino Dr.
Kanata, ON, CANADA
K2M 1M1
+1-613-592-9438
ak - 15 Dec 2003 20:52 GMT
> Does anyone know why the argument of setActionCommand (for example in
> JButton) is a String rather than an Object?
it comes from awt.
Button's label is its actionCommand.
--
____________
http://reader.imagero.com the best java image reader.
Bryan E. Boone - 16 Dec 2003 01:31 GMT
Not nessessarily.
The action command string is it's label only if one isn't explicitly set
using setActionCommand.
I usually explicitly set it because you can't really key off of an
internationalized
button label. It can change based on localization.
-Bryan
> > Does anyone know why the argument of setActionCommand (for example in
> > JButton) is a String rather than an Object?
[quoted text clipped - 7 lines]
>
> http://reader.imagero.com the best java image reader.
Gregory A. Swarthout - 16 Dec 2003 00:27 GMT
> Does anyone know why the argument of setActionCommand (for example in
> JButton) is a String rather than an Object?
[quoted text clipped - 3 lines]
> could be any kind of Object it could be an Integer, in which case I could
> use a switch statement to interpret its value rather than if-then-else.
A switch statement only works with int's and int's are not objects.
> I know that this interface is not going to change, I am just curious as to
> why I have to program in a non-obvious way.
Would the answer matter?
Greg
ak - 16 Dec 2003 11:02 GMT
> > Since the only thing that I ever see done with the value of actionCommand is
> > to compare its reference it could be any kind of Object. Furthermore if it
> > could be any kind of Object it could be an Integer, in which case I could
> > use a switch statement to interpret its value rather than if-then-else.
>
> A switch statement only works with int's and int's are not objects.
I think that Jim mean under 'value' Integer.intValue() and it is an int.
--
____________
http://reader.imagero.com the best java image reader.
Jon A. Cruz - 05 Jan 2004 18:01 GMT
> Does anyone know why the argument of setActionCommand (for example in
> JButton) is a String rather than an Object?
>
> Since the only thing that I ever see done with the value of actionCommand is
> to compare its reference it could be any kind of Object.
Then the code you saw was fragile, and not very good.
You should instead see something like
String cmd = evt.getActionCommand();
if ( "DO_FOO".equals(cmd) )
or
if ( Commands.DO_FOO.equals(cmd) )
Another thing a string action command can be used for is as a key to a
hashtable of objects.
Object magicThing = commandTable.get(cmd);
Or it could be passed on to an implementation engine (handy for
scripting also)
engine.doCommand( cmd );