在我的应用程序中,我正在读取.xml文件并将数据写入JTable中。除了表格的数据之外,.xml文件还包含一个定义每行背景颜色的属性。我对细胞渲染的方法看起来是这样的:在Java中投射错误
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; }
.xml文件在程序中被初始化。我的问题是,我不知道如何定义文件中的颜色,以便变量测试可以将其保存为一种颜色。我试着把它写成“Color.white”,“white”,甚至是“255,255,255”,但是当我试着将它保存在变量中时,我得到了一个转换错误。
任何想法如何定义文件中的颜色?
我想你想投行对象为Color对象.. – Rob
没有...我将正确的对象......这是我在列,并从中获得唯一的颜色属性...然后我尝试投它..这不工作 – schmimona
如果测试是一个字符串,我会写测试= go.getValueByName(“COLOR”)。toString();它会工作...但是我不能从一个字符串设置组件的背景,我可以吗? – schmimona