| Author |
converting a string color to an object color
|
Kee Kee moon
Ranch Hand
Joined: Dec 11, 2009
Posts: 140
|
|
Would any one please shed a light.
how to convert a String like:
String colorStr = "Red";
to an Object Color.red
Thanks.
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4089
|
|
is this what you were looking for?
|
SCJP
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
|
A Map would be better than two separate arrays, then you could have a getColour method which took a String as a parameter and use that as a key into the Map. The user wouldn't have to know the index of each of the colours then.
|
Joanne
|
 |
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 4089
|
|
|
joanne, i would marry you if i could
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
Randall Twede wrote:joanne, i would marry you if i could
I think Campbell is first in line at the moment, but feel free to try and push in front.
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32670
|
|
Joanne Neal wrote: . . . I think Campbell is first in line at the moment, but feel free to try and push in front.
Oh, Joanne, how sweet of you.
As long as Ruth doesn’t find out. And as long as Eleanor (my smaller daughter) doesn’t think I am marrying somebody younger than herself
|
 |
Paul Clapham
Bartender
Joined: Oct 14, 2005
Posts: 16483
|
|
(blushing) Bringing this thread back on-topic... the other part of the answer to the original question is "No, you may find it surprising, but there is nothing in the standard Java API to convert strings to Color objects, at least, not in the way you describe."
|
 |
Kee Kee moon
Ranch Hand
Joined: Dec 11, 2009
Posts: 140
|
|
Thank you very much that is exactly what I was looking for.
Randall Twede wrote:is this what you were looking for?
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4164
|
|
If you want to map the named constants of the Color class (or any other) which are instances of the same class, here's a utility I wrote some time back that uses Reflection to do it.Usage to obtain a Map of static Color fields of the Color class:
edit: Thought I should mention that the class is abstract to force instances to be named or anonymous subclasses, which makes it possible to obtain the ParameterizedType from the genericSuperclass.
|
luck, db
There are no new questions, but there may be new answers.
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
Your class is missing the following three methods:
The first two are required as per the contract of java.util.Map.
I actually don't like this trick. Instead of an entire class I'd use one static utility method instead:
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
Darryl Burke
Bartender
Joined: May 03, 2008
Posts: 4164
|
|
Rob Spoor wrote:Your class is missing the following three methods:
The first two are required as per the contract of java.util.Map.
Thanks Rob, for pointing that out. I'll keep that in mind if I ever publish the class.
Rob Spoor wrote:I actually don't like this trick. Instead of an entire class I'd use one static utility method instead:
I actually had this approach too, and the code is very similar to yours (but yours is more complete in that it checks the modifiers)I didn't think of using Class#cast(...) instead of suppressing the warning.
|
 |
 |
|
|
subject: converting a string color to an object color
|
|
|