views:

17

answers:

1

I have created my own TableCellEditor for a JTable column to do some special stuff while editing the cells of this column.

In that TableCellEditor i define a color when a cell in that column is hovered, like this :

public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {  
    if( isSelected )  // User clicked on this cell.
        setBackground( selectedRowBG );
    else if( rowIndexToHighlight == row  )  // user is hovering on this cell.
        setBackground( hoveredRowBG );
    else  // Set default cell color.
        setBackground( unHoveredRowBG );

    return this;
}

My problem is when i hover with the mouse on a cell in that special column the cell background color becomes "hoveredRowBG", then if i move with the mouse to a cell in another column or move to empty space in the table(That has no rows), the special cell bgColor still has the "hoveredRowBG" color. I want to remove that hovering color when such action happens.

Any ideas?

+2  A: 

Just store the actual highlighted row somewhere and test, while hovering, if it has changed. If yes, unhighlight the last highlighted and store the actual one. lastHighlightedRow is a field in my example, you may need another place to store the value.

public Component getTableCellEditorComponent(JTable table, Object value,boolean isSelected, int row, int column) {  
    if( isSelected )  // User clicked on this cell.
        setBackground( selectedRowBG );
    else if( rowIndexToHighlight == row  ) {  // user is hovering on this cell.
        if (!(lastHighlightedRow == this)) {
           lastHighlightedRow.setBackground(unHoveredRowBG);
           lastHighlightedRos = this;
        }
        setBackground( hoveredRowBG );
    }

    return this;
}
Andreas_D
Thank you ... My code was a bit complicated so i did some edits for this to work, but it is very helpful.
Brad