Results 1 to 8 of 8
  1. #1
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default type cast problem

    I have got a:

    javax.swing.text.StyleConstants$ColorConstants

    when I to-string it, I get for instance:

    java.awt.Color[r=255,g=0,b=0]

    But I'd like to go back to the type Color.
    how is that possible?

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default Re: type cast problem

    Not with toString, unless you WANT to write some regex to get those rgb values.

    Why are you using toString? Are you trying to save preferences or something?

    As an option, take a look at getRGB and the constructor that takes a single int.

  3. #3
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: type cast problem

    This does it:

    Java Code:
    final Pattern pattern = Pattern.compile("[=,\\]]");
            final String[] result = pattern.split(colorstr);
             Color c = new Color(Integer.parseInt(result[1]),
                     Integer.parseInt(result[3]),Integer.parseInt(result[5]));
    (I don't like reggae)

  4. #4
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: type cast problem

    I have javax.swing.text.StyleConstants$ColorConstants
    I can to-string-it but I do not need it, you are right!
    But how do I get javax.swing.text.StyleConstants$ColorConstants back to color?

  5. #5
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: type cast problem

    You don't need to parse anything. If you have the color,you can get the values through method calls. If you want to get the actual name of the color, then you can do it this way (although somewhat kludgey).

    Java Code:
    Field[] fields = Color.class.getDeclaredFields();
    Map<Object, String> colors = new HashMap<>();
    for (Field f : fields) {
     if (f.getModifiers() == 25) { // public(1), static(8) and final(16)
        try {
           colors.put(f.get(null), f.getName());
        }
        catch (IllegalAccessException ie) {
           ie.printStackTrace();
        }
     }
    }
    for (Object colorVal : colors.keySet()) {
     System.out.println(colors.get(colorVal) + " = " + colorVal);
    }
    I would recommend you simply use this to create a fixed map inside the class as opposed to doing it dynamically. If and when you create your own colors, you can also add them to the map.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  6. #6
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: type cast problem

    Right, jim, this is what I did:

    Java Code:
    public LinkedHashMap getColorList() {
             colorMap = new LinkedHashMap<String, Color>();
             colorMap.put("Black", Color.black);
             colorMap.put("Red", Color.red);
             colorMap.put("Green", Color.green);
             colorMap.put("Blue", Color.blue);
             colorMap.put("Yellow", Color.yellow);
             colorMap.put("Light Grey", Color.lightGray);
             colorMap.put("Grey", Color.gray);
             colorMap.put("Dark Grey", Color.darkGray);
             colorMap.put("Orange", Color.orange);
             colorMap.put("Magenta", Color.magenta);
             colorMap.put("Pink", Color.pink);
             colorMap.put("Cyan", Color.cyan);
             colorMap.put("White", Color.white);
    
             return colorMap;
        }
    My above method is working (post 3) though a little clumsy.....

  7. #7
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,620
    Rep Power
    5

    Default Re: type cast problem

    Do you always want to initialize and return a newly created list when someone calls the method? Why not initialize it elsewhere and just return the list itself. If you are worried about users modifying the list you can return a copy or use an immutable list.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  8. #8
    willemjav is offline Senior Member
    Join Date
    Dec 2007
    Location
    Spain
    Posts
    1,075
    Rep Power
    8

    Default Re: type cast problem

    I have a base class for all font materials and I invoke that stuff basically only once, like this
    *some more of the same):

    Java Code:
    public String[] getAllFontNames() {
            GraphicsEnvironment e =
                                GraphicsEnvironment.getLocalGraphicsEnvironment();
            String[] fontNameList = e.getAvailableFontFamilyNames();
            return fontNameList;
        }
    
        public FontStyleKeyValue[] getAllFontStyles() {
            fontstyleKeyValue = new FontStyleKeyValue[] {
             //   new FontStyleKeyValue("Regular", "plain.gif", Font.PLAIN),
            new FontStyleKeyValue("font-bold", "Bold", "bold.gif", Font.BOLD),
            new FontStyleKeyValue("font-italic", "Italic", "italic.gif", Font.ITALIC),
            new FontStyleKeyValue("font-underline", "Underlined", "line.gif", FONT_UNDERLINED)};
         //   new FontStyleKeyValue("font-lined", "Lined", "line2.gif", FONT_LINED)};
            return fontstyleKeyValue;
        } 
    
        
    
        public String[] getAllFontSizes() {
            String[] fontSizeList = new String[18];
            int count=0;
            for (int i = 6; i <= 13; i++) {
                fontSizeList[count] = "" + i;
                count++;
            }
            for (int i = 14; i <= 28; i += 2) {
                fontSizeList[count] = "" + i;
                count++;
            }
            fontSizeList[15] = "36";
            fontSizeList[16] = "48";
            fontSizeList[17] = "72";
    
            return fontSizeList;
        }
    Than I push the references of the maps and arrays around....

Similar Threads

  1. Type cast in case of classes
    By bigo005 in forum New To Java
    Replies: 4
    Last Post: 12-10-2013, 08:04 PM
  2. Type safety Unchecked cast
    By superhaNds in forum New To Java
    Replies: 9
    Last Post: 11-25-2013, 06:26 PM
  3. Menu Type cast Exception
    By arulmozs in forum AWT / Swing
    Replies: 8
    Last Post: 11-02-2009, 10:34 PM
  4. [SOLVED] Cast string type to int type
    By GilaMonster in forum New To Java
    Replies: 9
    Last Post: 09-17-2008, 10:43 AM
  5. Help determing NamingEnumeration type/cast
    By BlankMan in forum Eclipse
    Replies: 1
    Last Post: 09-05-2008, 07:50 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •