我创建了一个表,其中一个参数是颜色,就像这样
我没有从我创建的表模型中覆盖 getValue 方法,这意味着当我获得颜色值时,它们以对象的形式出现。如果我将它们存储为对象或字符串,我将得到如下值:
java.awt.Color[r=255,g=0,b=0]
这是我的表格模型,以防万一:
private static final Object[][] DATA = {
{ "1", "Task 1", new Integer(10), false, new Integer(0), Color.red },
{ "2", "Task 2", new Integer(10), false, new Integer(0), Color.blue },
{ "3", "Task 3", new Integer(10), false, new Integer(0),
Color.green },
{ "4", "Task 4", new Integer(10), false, new Integer(0),
Color.orange },
{ "5", "Task 5", new Integer(10), false, new Integer(0),
Color.black } };
private MyTableModel myTableModel = new MyTableModel(DATA);
class MyTableModel extends DefaultTableModel {
private static final String[] COLUMN_NAMES = { "Station #", "Name",
"avg Time", "Buffer", "Buffer Parts", "Color" };
public MyTableModel(Object[][] data) {
super(data, COLUMN_NAMES);
}
@Override
public Class getColumnClass(int c) {
//System.out.println("Class for " + c + ": " + getValueAt(0, c).getClass().toString());
return getValueAt(0, c).getClass();
}
@Override
public boolean isCellEditable(int row, int col) {
if (col == 0) {
return false;
} else if (col == 4) {
boolean di = (Boolean) getValueAt(row, (col - 1));
if (!di) {
return false;
} else {
return true;
}
} else {
return true;
}
}
public void printDebugData() {
int numRows = getRowCount();
int numCols = getColumnCount();
for (int i = 0; i < numRows; i++) {
System.out.print(" row " + i + ":");
for (int j = 0; j < numCols; j++) {
Object datum = getValueAt(i, j);
// System.out.print(" " + data[i][j]);
System.out.print(" " + datum);
}
System.out.println();
}
System.out.println("--------------------------");
}
}
表中的所有值都存储在哈希图中,这些值传递给另一个类,我需要在其中创建一个颜色向量左右来存储表上设置的颜色。
Station 1=[1, Task 1, 10, false, 0, java.awt.Color[r=255,g=0,b=0]]
Station 2=[2, Task 2, 10, false, 0, java.awt.Color[r=0,g=0,b=255]]
Station 3=[3, Task 3, 10, false, 0, java.awt.Color[r=0,g=255,b=0]]
Station 4=[4, Task 4, 10, false, 0, java.awt.Color[r=255,g=200,b=0]]
Station 5=[5, Task 5, 10, false, 0, java.awt.Color[r=0,g=0,b=0]]
我环顾四周,但找不到任何信息,那么处理它的最佳方法是什么,以便我可以存储要与传递的值一起使用的颜色?
我希望我说清楚了,如果需要任何其他信息,请告诉我。