I have another solution to the problem above. In my case, I had a JTable containing 10 columns. I only wished to highlight a certain subset of cell rows based on a Mark entered. In the example screenshot appended below I entered the Mark 167 and pressed the Find All button. In so doing, the cell renderer highlighted a subset of cells. Namely, the Script No, Candidate, Mark and Grade
In order to achieve this, I created a cell renderer to extend the DefaultCellRenderer. I've taken out some code that is unrelated to the problem and so it may not compile. Nonetheless, you get an overview of the solution to your problem:
import java.awt.Component;
import java.awt.Color;
import javax.swing.JTable;
import javax.swing.table.DefaultTableCellRenderer;
/*
*
* This class is the Color Renderer for the "Find All" Button.
* It basically highlights all of the scripts with a certain color
* This renderer is responsible Highlighting the JTable.
* Based on the particular Mark that has been entered in the find all
* text field
*
*
*/
public class MarkSearchColorCellRenderer extends DefaultTableCellRenderer
implements ColorCellRenderer, GradeValidation
{
public MarkSearchColorCellRenderer(int aMark, Color aColor)
{
the_Mark = aMark;
theColor = aColor;
}
@Override
public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column)
{
Component cell = super.getTableCellRendererComponent(table,
value, isSelected, hasFocus, row, column);
setHorizontalAlignment(RIGHT);
if ( column == 0 ) // script no column
{
//check the current Mark within this row
//i.e. for this particular candidate
Object curMark = table.getValueAt(row, column+2);
//highlight this cell a certain color if the
//search mark has been found for this particular candidate
highlightScripts(curMark, cell);
}
else if(column == 1) // Candidate No. Column
{
Object curMark = table.getValueAt(row, column+1);
highlightScripts(curMark, cell);
}
else if ( column == 2 ) // mark column
{
Object curMark = value;
highlightScripts(curMark, cell);
}
else if (column == 3) // Grade Column
{
setHorizontalAlignment(LEFT);
Object curMark = table.getValueAt(row, column-1);
highlightScripts(curMark, cell);
}
else if (column == 5) // script no column
{
Object curMark = table.getValueAt(row, column+2);
highlightScripts(curMark, cell);
}
else if( column == 6) // Candidate No. Column
{
Object curMark = table.getValueAt(row, column+1);
highlightScripts(curMark, cell);
}
else if ( column == 7) // mark column
{
Object curMark = value;
highlightScripts(curMark, cell);
}
else if ( column == 8) // Grade Column
{
setHorizontalAlignment(LEFT);
Object curMark = table.getValueAt(row, column-1);
highlightScripts(curMark, cell);
}
else
{
// Should never arrive here.
// If it does, then you've rendered a column that may not
// function with this class
// therefore, I'm going to exit the program
//System.exit(0);
}
table.repaint();
return cell;
} // end getTableCellRendererComponent()
// Change the color of this cell if the aValue matches
// the value within the particular cell
private void highlightScripts(Object aValue, Component aCell)
{
Object value = aValue;
Component cell = aCell;
if ( value != null )
{
if (value instanceof Integer )
{
Integer amount = (Integer) value;
if( amount.intValue() == the_Mark )
{
cell.setBackground( theColor );
}
else
{
cell.setBackground( Color.white );
}
}
}
else
{
cell.setBackground( Color.white );
}
}// end highlightScripts()
}//end class
/*
*
* Inside the "Find All" button listener you would attach the
* MarkSearchColorCellRenderer to selected JTable columns.
* A snippet of the code is as follows:
*
*/
// select a color
Color myMarkColor = new Color(226,182,90);
// the mark to search
Integer markToSearch =
new Integer(find_mark_jTextField.getText());
//an instance of the class MarkSearchColorCellRenderer (outlined above)
MarkSearchColorCellRenderer my_MarkSearch_CellRenderer =
new MarkSearchColorCellRenderer(
markToSearch.intValue(),myMarkColor);
/*
*
* Get the all the Table columns of the JTable on which you wish to assign
* the particular cell renderer. In my example I have 8 table column's
* that I wish to assign the cell renderer
* i.e. Script, Candidate, Mark, Grade Script, Candidate, Mark, Grade
*
* On looking at the screenshot I've ignored the monitor column (check box)
*/
TableColumn script_column_100_A = first_100_Table.getColumnModel().getColumn(0);
TableColumn candidate_column_100_A = first_100_Table.getColumnModel().getColumn(1);
TableColumn mark_column_100_A = first_100_Table.getColumnModel().getColumn(2);
TableColumn grade_column_100_A = first_100_Table.getColumnModel().getColumn(3);
TableColumn script_column_100_B = first_100_Table.getColumnModel().getColumn(5);
TableColumn candidate_column_100_B = first_100_Table.getColumnModel().getColumn(6);
TableColumn mark_column_100_B = first_100_Table.getColumnModel().getColumn(7);
TableColumn grade_column_100_B = first_100_Table.getColumnModel().getColumn(8);
/*
*
* assign the MarkSearchColorCellRenderer to each of the choosen table column's.
*
* i.e. Script, Candidate, Mark, Grade Script, Candidate, Mark, Grade
*
*/
script_column_100_A.setCellRenderer(my_MarkSearch_CellRenderer );
script_column_100_A.setCellRenderer(my_MarkSearch_CellRenderer );
candidate_column_100_A.setCellRenderer(my_MarkSearch_CellRenderer );
mark_column_100_A.setCellRenderer(my_MarkSearch_CellRenderer );
grade_column_100_A.setCellRenderer(my_MarkSearch_CellRenderer );
script_column_100_B.setCellRenderer(my_MarkSearch_CellRenderer );
candidate_column_100_B.setCellRenderer(my_MarkSearch_CellRenderer );
mark_column_100_B.setCellRenderer(my_MarkSearch_CellRenderer );
grade_column_100_B.setCellRenderer(my_MarkSearch_CellRenderer );
this.repaint();