Java JTable 备用行颜色不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17762214/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 14:54:36  来源:igfitidea点击:

Java JTable Alternate Row Color not working

javaswingjtabletablecellrenderer

提问by Iqbal Hossain

Why this following code not working? where is the problem? My jTable is initiated as jTable1;

为什么以下代码不起作用?哪里有问题?我的 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;
            };

        });

回答by bbhar

Recently while going through the source code of javax.swing.table.DefaultTableCellRenderer, I found following simple solution which will provide alternate row coloring for all tables in an application.

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

In the code, just after setting default look and feel insert following code:

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

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

回答by mKorbel

回答by Joop Eggen

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;
        };
    });

The main error is the querying of the table for its renderer. If you have other column renderers you have to solve it there too.

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

回答by Iqbal Hossain

The Correct answer is as follows for me...

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

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;
            }

        });

回答by Ganesh Rengarajan

Try this one.... REFER

试试这个.... 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;
};