使用jtable创建了一个表格,并把表格中某些行设置了底色(用以区分特殊的数据),现在的问题是,当选择一个有底色的行时,该行底色变成了灰色,原来的底色看不见了,请问有什么解决方案。号小分不多,请见谅。

解决方案 »

  1.   

    改写JTable的CellRenderer
    public class TableViewRenderer extends DefaultTableCellRenderer {    private Color unselectedForeground;
        private Color unselectedBackground;    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,
                boolean hasFocus, int row, int column) {                if (isSelected) {
                        component.setForeground( table.getSelectionForeground());
                        component.setBackground(table.getSelectionBackground());
                    } else {
                        component.setForeground((unselectedForeground != null) ? unselectedForeground
                                                : table.getForeground());
                        component.setBackground((unselectedBackground != null) ? unselectedBackground
                                                : table.getBackground());
                    }
                setFont(table.getFont());
                setValue(value);
                return this;
            }
        }
    }
      

  2.   

    通过setForeground()和setBackground()两个方法,是把表格中的字的颜色改变了。而实际的情况是当选择一行时,该行底色变成了灰色,把以前的底色给当住了,现在是要取消那种默认的选择行变灰色。。
      

  3.   

    if (isSelected) { 
                        component.setForeground( Color ); 
                        component.setBackground(Color);  //这里的颜色设定你选中时想要的颜色
                    } else { 
                        component.setForeground(); 
                        component.setBackground();  // 这里的颜色设定你不选中时想要的颜色
                    } 
    setBackground()肯定是改变背景色,肯定是要改CellRenderer,具体的代码就要根据你的实际功能来写了