File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes converting a string color to an object color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "converting a string color to an object color" Watch "converting a string color to an object color" New topic
Author

converting a string color to an object color

Kee Kee moon
Ranch Hand

Joined: Dec 11, 2009
Posts: 147

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: 4347
    
    2

is this what you were looking for?


SCJP
Visit my download page
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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: 4347
    
    2

joanne, i would marry you if i could
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
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: 40052
    
  28
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: 18987
    
    8

(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: 147
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: 4664
    
    5

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: 19784
    
  20

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 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4664
    
    5

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
 
subject: converting a string color to an object color