The following code should get you going in the right direction if you want to set a row colour. Please note that this code sets *column* colour programmatically so you will have to adapt it a bit to make it work for rows. I've never tried to colour rows so I don't know if it is a big change or a little one.
 
This first method is in my Table Model and sets the desired background and foreground colours of the column to red and white respectively but only for specific columns:
 

/**

* Set the colours of read-only columns so that they are different than the colours of editable columns.

*

*/

public void setColumnColors() {

/* Set the colours of a read-only column so that they are different than an editable column. */

for (int ix=0; ix<columnMetaData.size(); ix++) {

if (!isCellEditable(0,ix)) { //if column is read-only

TableColumn column = table.getColumnModel().getColumn(ix);

DefaultTableCellRenderer myColumnRenderer = new DefaultTableCellRenderer();

myColumnRenderer.setBackground(Color.red); //set an arbitrary background colour

myColumnRenderer.setForeground(Color.white); //set an arbitrary foreground colour

myColumnRenderer.setFont(new Font(myColumnRenderer.getFont().getName(), Font.ITALIC, myColumnRenderer.getFont().getSize())); //set font bold??

column.setCellRenderer(myColumnRenderer);

}

}

}

The following method, also in my TableModel, determines if the cell should be set to these colours:
 

/**

* Determine which cells are editable. Actually, this method decides which COLUMNS are editable since all cells in a given column

* should either be editable or not regardless of which row they occupy.

*

* @return boolean

* @param rowno int

* @param colno int

*/

public boolean isCellEditable(int rowno, int colno) {

boolean retval = false;

/* Determine the name of the column whose position number was passed via the column parameter. */

String ColumnInQuestion = new String(getColumnName(colno));

/* If the table has no primary key or unique key, don't allow any changing of this table since we won't have any way to ensure that the

right row and only the right row gets changed. */

if (vecPKColumnNames.size() == 0 && vecUKInfo.size() == 0) { //table has no primary key and no unique key

if (DEBUG) System.out.println("Column " + ColumnInQuestion + " is part of a table with no primary key and no unique keys. No columns in any such table can be edited.");

retval = false;

toolkit.beep();

}

else { //table has a primary key or unique key

/* If the table has a primary key or unique key and is a parent in a referential constraint, allow updates of all columns EXCEPT the primary

key or unique key columns. */

if (isTableAParent) { //table is a parent

if (vecPKColumnNames.contains(ColumnInQuestion) || vecUKInfo.contains(ColumnInQuestion)) { //if user is trying to update the primary key or unique key column, say no

if (DEBUG) System.out.println("Column " + ColumnInQuestion + " is part of a Primary Key or a Unique Key in a table that is a parent so it cannot be edited.");

retval = false; //Primary Key and Unique Key columns should not be editable

toolkit.beep();

}

else { //if user is trying to update a non-PK, non-UK column, say yes

if (DEBUG) System.out.println("Column " + ColumnInQuestion + " is part of a table that is a parent but it is not part of a Primary Key or a Unique Key so it can be edited.");

retval = true;

}

}

else { //table is not a parent in a referential constraint

if (DEBUG) System.out.println("Table has a Primary Key or a Unique Key but is not a parent so every column should be editable.");

retval = true;

}

}

return(retval);

}

The application that owns the TableModel simply does this line to set the column colours:
SATableModel mySAModel.setColumnColors();
 
I hope this helps you get on the right track.
 

Rhino
---
rhino1 AT sympatico DOT ca
"If you want the best seat in the house, you'll have to move the cat."

Reply via email to