File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

converting a string color to an object color

 
Kee Kee moon
Ranch Hand
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 4353
2
Java
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
is this what you were looking for?
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Randall Twede
Ranch Hand
Posts: 4353
2
Java
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
joanne, i would marry you if i could
 
Joanne Neal
Rancher
Pie
Posts: 3742
16
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 47310
52
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Pie
Posts: 20208
26
MySQL Database
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
(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
Posts: 147
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5116
11
Java Netbeans IDE Opera
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Rob Spoor
Sheriff
Pie
Posts: 20399
47
Chrome Eclipse IDE Java Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Darryl Burke
Bartender
Posts: 5116
11
Java Netbeans IDE Opera
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic