Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / January 2004

Tip: Looking for answers? Try searching our database.

Why is the argument of setActionCommand a String?

Thread view: 
Jim Cobban - 15 Dec 2003 19:19 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.  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 );


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.