如何更改所选单元格的整列和整行的背景

时间:2011-10-29 11:57:26

标签: java swing jtable tablecellrenderer

我有一个jtable,我想在选择一个单元格来改变整个行的背景和整个单元格的整个列时(不是仅改变单元格的背景!),该怎么做?

请告知,谢谢。

1 个答案:

答案 0 :(得分:3)

我使用以下方法来控制列的颜色。要了解如何合并当前单元格,请查看 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;
        }
    };