【发布时间】:2011-12-06 04:50:37
【问题描述】:
在我的应用程序中,我正在读取 .xml 文件并将数据写入 JTable。除了表的数据之外,.xml 文件还包含一个定义每行背景颜色的属性。我的单元格渲染方法如下所示:
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) { JComponent comp = new JLabel(); 如果(空!=值){ //读取数据并将其写入comp } GenericTableModel 模型 = (GenericTableModel) table.getModel(); GenericObject go = model.getRowObject(row); 颜色测试 = 新颜色(255、255、255); if (go.getValueByName("COLOR") == null){ }别的{ test =(Color) go.getValueByName("COLOR"); } comp.setBackground(测试); 返回补偿; }.xml 文件在程序中初始化。我的问题是我不知道如何在文件中定义颜色,以便变量 test 能够将其保存为颜色。我尝试将其写为“Color.white”、“white”甚至“255、255、255”,但是当我尝试将其保存在变量中时出现转换错误。
关于如何在文件中定义颜色的任何想法?
【问题讨论】:
-
我认为您正在尝试将 Row 对象转换为 Color 对象..
-
不...我正在添加正确的对象...我正在获取该行并仅从中获取颜色属性...然后我尝试投射它..这不起作用
-
如果 test 是一个字符串,我会写 test = go.getValueByName("COLOR").toString();它本来可以工作的...但是我不能从字符串中设置组件的背景,可以吗?