Java JTable Alternate Row Color无法正常工作

时间:2013-07-20 12:21:40

标签: java swing jtable tablecellrenderer

为什么以下代码不起作用?问题出在哪儿? 我的jTable是作为jTable1;

启动的
jTable1.setDefaultRenderer(Object.class,new TableCellRenderer(){

            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = (Component) table.getCellRenderer(row, column);
                c.setBackground(row%2==0 ? Color.white : Color.yellow);                        
                return c;
            };

        });

6 个答案:

答案 0 :(得分:18)

最近在浏览javax.swing.table.DefaultTableCellRenderer的源代码时,我发现了以下简单的解决方案,它将为应用程序中的所有表提供备用行着色。

在代码中,在设置默认外观之后插入以下代码:

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
if (defaults.get("Table.alternateRowColor") == null)
    defaults.put("Table.alternateRowColor", new Color(240, 240, 240));

答案 1 :(得分:5)

答案 2 :(得分:4)

jTable1.setDefaultRenderer(Object.class, new DefaultTableCellRenderer() {

        @Override
        public Component getTableCellRendererComponent(JTable table, 
                Object value, boolean isSelected, boolean hasFocus,
                int row, int column) {
            Component c = super.getTableCellRendererComponent(table, 
                value, isSelected, hasFocus, row, column);
            c.setBackground(row%2==0 ? Color.white : Color.yellow);                        
            return c;
        };
    });

主要错误是查询表的渲染器。如果你有其他的列渲染器,你也必须在那里解决它。

答案 3 :(得分:2)

对我来说,正确答案如下......

jTable1.setDefaultRenderer(Object.class, new TableCellRenderer(){
            private DefaultTableCellRenderer DEFAULT_RENDERER =  new DefaultTableCellRenderer();
            @Override
            public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
                Component c = DEFAULT_RENDERER.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
                if (row%2 == 0){
                    c.setBackground(Color.WHITE);
                }
                else {
                    c.setBackground(Color.LIGHT_GRAY);
                }                        
                return c;
            }

        });

答案 4 :(得分:1)

试试这个.... REFER

JTable table = new JTable(){
    public Component prepareRenderer(TableCellRenderer renderer, int row, int column){
        Component returnComp = super.prepareRenderer(renderer, row, column);
        Color alternateColor = new Color(252,242,206);
        Color whiteColor = Color.WHITE;
        if (!returnComp.getBackground().equals(getSelectionBackground())){
            Color bg = (row % 2 == 0 ? alternateColor : whiteColor);
            returnComp .setBackground(bg);
            bg = null;
        }
        return returnComp;
};

答案 5 :(得分:1)

最简单的方法是:

UIDefaults defaults = UIManager.getLookAndFeelDefaults();
defaults.putIfAbsent("Table.alternateRowColor", Color.LIGHT_GRAY);

这将影响您班级中的所有表。