Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Changing background color in JTable, only changes one row at a time...

843807Apr 26 2010 — edited Apr 26 2010
I'm trying to change the color of rows when the 5th column meets certain criteria. I think I'm very close, but I've hit a wall.

What's happening is the row will change color as intended when the text in the 5th column is "KEY WORD", but when I type "KEY WORD" in a different column it will set the first row back to the regular colors. I can easily see why it's doing this, everytime something is changed it rerenders every cell, and the listener only checks the cell that was just changed if it met the "KEY WORD" condition, so it sets every cell (including the previous row that still meets the condition) to the normal colors. I can't come up with a good approach to changing the color for ALL rows that meet the condition. Any help would be appreciated.

In this part of the CellRenderer:
        if (isSelected)
            color = Color.red;
        else
            color = Color.blue;
            
        if (hasFocus)
            color = Color.yellow;

        //row that meets special conditions
        if(row == specRow && col == specCol)
            color = color.white; 
I was thinking an approach would be to set them to their current color except for the one that meets special conditions, but the two problems with that are I can't figure out how to getColor() from the table, and I'm not sure how I would initially set the colors.

Here's the rest of the relevant code:
    public void tableChanged(TableModelEvent e)
    {
        int firstRow = e.getFirstRow();
        int lastRow  = e.getLastRow();
        int colIndex = e.getColumn();

        if(colIndex == 4)
        {
            String value = (String)centerTable.getValueAt(firstRow, colIndex);
            // check for our special selection criteria
            if(value.equals("KEY WORD"))
            {
                for(int j = 0; j < centerTable.getColumnCount(); j++)
                {
                    CellRenderer renderer =
                        (CellRenderer)centerTable.getCellRenderer(firstRow, j);
                    renderer.setSpecialSelection(firstRow, j);
                }
            }
        }
    }
import javax.swing.table.*;
import javax.swing.*;
import java.awt.Component;
import java.awt.Color;

public class CellRenderer extends DefaultTableCellRenderer
{
    int specRow, specCol;
    
    public CellRenderer()
    {
        specRow = -1;
        specCol = -1;
    }
 
    public Component getTableCellRendererComponent(JTable table,
                                                   Object value,
                                                   boolean isSelected,
                                                   boolean hasFocus,
                                                   int row, int col)
    {
        setHorizontalAlignment(JLabel.CENTER);
        
        Color color = Color.green;

        if (isSelected)
            color = Color.red;
        else
            color = Color.blue;
            
        if (hasFocus)
            color = Color.yellow;

        if(row == specRow && col == specCol)
            color = color.white; 
        
        //setForeground(color);
        setBackground(color);
        setText((String)value);
        return this;
    }
 
    public void setSpecialSelection(int row, int col)
    {
        specRow = row;
        specCol = col;
    }
}
If I'm still stuck and more of my code is needed, I'll put together a smaller program that will isolate the problem tomorrow.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on May 24 2010
Added on Apr 26 2010
4 comments
881 views