Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6673517
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:38:10+00:00 2026-05-26T03:38:10+00:00

In my application, I am reading a .xml file and writing the data in

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T03:38:11+00:00Added an answer on May 26, 2026 at 3:38 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

While implementing XML file reading/writing in my application I saw that when I call
My asp.net web application is reading and decrypting the password from a XML file
I am reading data from an XML file, and I want to store that
For a bit of background, I'm writing a meter reading application in C for
My application is SL2 reading and writing data through an Entity Framework Model exposed
I have an application where I am reading and writing small blocks of data
I am developing an application which requires the reading and writing of XML files.
In my application I have a simple XML formatted file containing structured data. Each
I have a custom file type that I'm reading from and writing to within
I'm reading an xml file generated by a 3rd-party application that includes the following:

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.