tags:

views:

45

answers:

1

Hello everyone, I am a newbie here just joined this wonderfull community. I have to admit this is the best site out there for programming questions and answers. Now let me get to the point:

I am trying to create a JList where I can input lines with different font colors taken from a ColorChooser. I have tried something already as described below

Thank you in advance for any answers. Best regards, dwc

Here is my sample code:

class CustomObject
{
    String s;
    Color color;
    String scolor;

    public CustomObject(Color color, String s)
    {
        this.s = s;
        this.color = color;
    }

    public String getColor()
    {
        return scolor = Integer.toString(color.getRGB());
    }

    public String getData()
    {
        return s;
    }

    @Override
    public String toString()
    {
        return s + color.getRGB();
    }

}

class myListRenderer extends DefaultListCellRenderer
{

Color color;

@Override
public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus)
{
    super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);

    if (value != null)
    {
        CustomObject o = (CustomObject)value;
        setText(o.getData());
        color = new Color(Integer.parseInt(o.getColor()));
        setForeground(color);
    }
    return this;
}
}

My main problem is that I get an error:

java.lang.ClassCastException: java.lang.String cannot be cast to app.CustomObject

in the line:

CustomObject o = (CustomObject)value;
+1  A: 

If you want each line to have a different background color then you need to store that information in the model. So instead of simply storing a String in the model you store a custom Object that contains two properties, the Color and the Text. Then your custom renderer uses the Color to set the background of the line and the Text to set the value of the line.

This posting shows an example of using this approach. The example is for a combo box but the concept is identical.

camickr
camickr can you please show my how the custom renderer is going to work? I know I am asking too much but as I said I am kind of new to Java not so experienced in writing custom code...
devilwontcry
You where given a link to an example renderer that alternates the color of lines. So the code is not that much different. You cast the value to your "custom Object". Then you set the background using the Color of you custom Object. Then you set the text of the label using the text of your custom Object. If you need more help then post your SSCCE (http://sscce.org). Note also that you should extend the default renderer, not a JLabel.
camickr
I am trying to get this to work right now, but I am facing some problems with this line:CustomObject o = (CustomObject)value;for some bizzare reason I get the exception:java.lang.ClassCastException: java.lang.String cannot be cast to app.CustomObject
devilwontcry
Well you can't add a String to the ListModel. You need to add a CustomObject to the model. I gave you a working example. I'm not a mind reader I can't guess what you are doing and you haven't posted a SSCCE, so I can't offer any more help.
camickr
Ok ok you are right, let me post my SSCCE then, do I just put it in a comment here or edit my first post?
devilwontcry