在我的應用程序中,我正在讀取.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