Search code examples
javaswingjtabletablecellrenderer

How to change the background for whole column and whole row of a selected cell


i have a jtable and i want when selecting a cell to change the background for the whole row and the whole column of this cell (not to change the background of the cell only !), how to do so ?

please advise, thanks.


Solution

  • I've used the following to control color of a column. To see how to incorporate the current cell, look at http://www.javaworld.com/javaworld/javaqa/2001-09/03-qa-0928-jtable.html

        this.table = new JTable()
        {
            private static final long serialVersionUID = -5739534894469353266L;
    
    
            /**
             * Set the background color of the row equal to the color of the path in the map
             */
            @Override
            public Component prepareRenderer( final TableCellRenderer renderer, final int Index_row, final int Index_col )
            {
                final Component comp = super.prepareRenderer( renderer, Index_row, Index_col );
                // even index, selected or not selected
    
                if ( Index_col == 1 )
                {
                    // Color column, match foreground / background colors
                    comp.setBackground( MyColors.getColor( Index_row ) );
                    comp.setForeground( MyColors.getColor( Index_row ) );
                }
                else
                {
                    comp.setBackground( Color.white );
                    comp.setForeground( Color.black );
                }
                return comp;
            }
        };