How to manipulate color values?

788 views Asked by At

I have created a table where one of the arguments is a color, like so

table

I haven't overriden the getValue method from the table model I've created which means when I get the color values they come as Objects. If I store them as objects, or strings i'll get the values as:

java.awt.Color[r=255,g=0,b=0]

Here's my table model, just in case:

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("--------------------------");
 }
}

All the values from the table are stored in hashmaps that are passed on to another class where I need to create a color vector or so to store the colors that had been set on the table.

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]]

I've looked around but couldn't find any information, so what's the best way to deal with it so I can store the colors to be used with values being passed?

I hope I made myself clear, if any other info is needed just let me know.

1

There are 1 answers

4
Ernest Friedman-Hill On

The value you get from getValueAt() is the actual java.awt.Color object; it's just that the return type of the method is Object. You need to cast the value:

Color color = (Color) tableModel.getValueAt(0, 5);

Now you have a Color variable pointing to the object, and you can do whatever you want with it.