简体   繁体   中英

Casting error in Java

In my application, I am reading a .xml file and writing the data in a JTable. Apart from the data for the table, the .xml file contains an attribute defining the background color of each row. My method for cell rendering looks something like this:

public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
        boolean hasFocus, int row, int col) {
    JComponent comp = new JLabel();

    if (null != value) {
        //reading the data and writing it in the comp
    }

    GenericTableModel model = (GenericTableModel) table.getModel();
    GenericObject go = model.getRowObject(row);

    Color test = new Color(255, 255, 255);
    if (go.getValueByName("COLOR") == null){

    }else{
        test =(Color) go.getValueByName("COLOR");
    }

    comp.setBackground(test);

    return comp;
}

The .xml file is initialized within the program. My problem is that I don't know how to define the color in the file so that the variable test will be able to save it as a color. I tried writing it as "Color.white", "white" and even "255, 255, 255" but i get a casting error when I try saving it in the variable.

Any ideas as to how could I define the color in the file?

I take it that GenericObject#getValueByName() returns a string, right? In that case you need to convert the string to something that can be used to create a Color instance. Assuming that the string is "R,G,B", then split the string on the comma, convert each component to an integer and create a color:

public static Color fromString(String rgb, Color deflt) {
    String[] comp = rgb.split(",");
    if (comp.length != 3)
        return deflt;
    int rc[] = new int[3];
    for (int i = 0; i < 3; ++i) {
        rc[i] = Integer.parseInt(comp[i].trim());
        if (rc[i] < 0 || rc[i] > 255)
            return deflt;
    }
    Color c = new Color(rc[0], rc[1], rc[2]);
    return c;
}

The other alternative is to define the color field with color names matching the predefined static fields in Color (Color.BLACK, Color.RED, etc), and use reflection to get the correct field, but I leave that as an excercise.

As a followup to forty-two's answer, it really depends on how the color is supposed to be stored in the XML. It would also be possible to save the color's value as a single string (no commas), representing either the decimal or hex value of the color. (Hex is more human-readable for colors, eg "FFFF00" for Yellow instead of "16776960")

eg as decimal (and with no error checking, for the record, I like default values like forty-two used)

public static Color readColor(String decimalString) {
   return new Color(Integer.parseInt(decimalString));
}

public String writeColor(Color color) {
    return Integer.toString(color.getRGB());
}

eg as hex (you need avoidOverflows to handle colors with alpha values like F0123456)

public static Color readColor(String hexString) {
    long avoidOverflows = Long.parseLong(hexString, 16);
    return new Color((int)long);
}

public String writeColor(Color color) {
    return Integer.toHexString(color.getRGB(), 16);
}

I've even seen the hex values preceded by a "#" to make them more HTML-like. So, it really depends on the spec for your XML.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM