Is there a simple way to get a list/array of all the predefined colors?
(without listing them all individually) ?

It seems as though the colors are properties of the class "Color" rather than an enumeration.

Yes, they are constants in the Color class. They look like this: public final static Color gray = new Color(128, 128, 128); You can use reflection to loop through all the variables in the class and select those which are public static final Colors - this code does that and returns a map of all the color names and the corresponding Color instances. Feel free to use it, as long as you leave the Author comment in place.

// get a map of all the Color constants defined in the Color class
   // Author: James Cherrill
   public static java.util.Map<String, java.awt.Color> getAllColors() {
      java.util.TreeMap<String, java.awt.Color> colors = 
         new java.util.TreeMap<String, java.awt.Color>();
      java.lang.reflect.Field[] fields = java.awt.Color.class.getDeclaredFields();
      for (java.lang.reflect.Field f : fields) {
         try {
            if (! java.lang.reflect.Modifier.isPublic(f.getModifiers())) break;
            if (! java.lang.reflect.Modifier.isStatic(f.getModifiers())) break;
            if (! java.lang.reflect.Modifier.isFinal(f.getModifiers())) break;
            Object o = f.get(null);
            if (o instanceof java.awt.Color) { 
               String name = f.getName();
               name = name.substring(0, 1).toUpperCase() +  
                      name.substring(1).toLowerCase(); // convert to title case
               name = name.replace("_",""); // de-duplicate Light_gray and Lightgray
               colors.put(name, (java.awt.Color) o);
            }
         } catch (Exception e) {
            e.printStackTrace();
         }
      }
      return colors;
   }

I do appreciate the effort but I'm not going to copy/paste it, I'll just learn to use reflection one day.

Fair enough - I has assumed that this wasn't a homework question, so I have no problem with you copying it as long as you don't claim it's yours! It may also give you a relevant little example of how to use reflection - its great fun.
J

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.