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;