jtable+tablemodel - cell foreground color change

Let me explain my problem first:

I have a JTable with a corresponding TableModel for the products that my computershop sells, wich looks something like this:

DESCR| AMOUNT_IN_STOCK | REORDER_LEVEL

mouse|20 |10

keyboard|9 |8

screen |3 |5

As you can see, the amount_in_stock for the screens is lower than its reorder_level.

What I want to do now is to put the cell amount_in_stock for screen to a foregroundcolor of RED.

I tried to to this with a Renderer, but I had a problem since I can get the amount_in_stock value but I don't know how I can get the corresponding reoder_level value.

Thanks in advance!

Under here I have past my code how I use my table and the code of my tableModel

...

ProductTableModel tableModel =new ProductTableModel(sc);

JTable table =new JTable(tableModel);

JScrollPane scrollPane =new JScrollPane(table);

....

publicclass ProductTableModelextends AbstractShopTableModelimplements Observer{

private List<Product> products;

private String[] columnNames ={"Description",

"Amount in stock",

"ReorderLevel",

"ReorderQuantity",

"Default selling price",

"VAT"};

private DecimalFormat decimalFormat =new DecimalFormat("0.0");// because of floating point problem

public ProductTableModel(ShopController controller){

products = controller.getAllProducts();

controller.registerAsProductObserver(this);

}

publicint getColumnCount()

{

return columnNames.length;

}

publicint getRowCount()

{

return products.size();

}

public String getColumnName(int col){

return columnNames[col].toString();

}

public Object getValueAt(int rowIndex,int columnIndex)

{

Product p = products.get(rowIndex);

switch( columnIndex )

{

case 0:return p.getDescription();

case 1:return p.getAmountInStock();

case 2:return p.getReorderLevel();

case 3:return p.getReorderQuantity();

case 4:return p.getSellingPrice();

case 5:return decimalFormat.format(p.getVatPercentage()*100)+"%";

default:returnnull;

}

}

public Class getColumnClass(int col){

return String.class;

}

public Object getObject(int index){

return getProduct(index);

}

public Product getProduct(int index){

return products.get(index);

}

/*Observer Pattern: what the ProductTableModel should do if it

recieves a notification that the list of products has changed.*/

publicvoid update(Observable o, Object arg){

fireTableDataChanged();/*makes the JTable update itself*/

}

};

[5414 byte] By [Bartelda] at [2007-11-27 1:12:04]
# 1
A renderer works.You can get the row object from the model.ProductTableModel tm = (ProductTableModel)table.getModel();Object reorderQuant = tm.getValueAt(row,column);
PhHeina at 2007-7-11 23:47:24 > top of Java-index,Desktop,Core GUI APIs...