0

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

请指教,谢谢。

4

1 回答 1

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;
        }
    };
于 2011-10-29T12:06:38.870 回答