简体   繁体   English

Java JTable 备用行颜色不起作用

[英]Java JTable Alternate Row Color not working

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

        });

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));
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.如果您有其他列渲染器,您也必须在那里解决它。

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

        });

The easiest way is:最简单的方法是:

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

This will affect all the tables in your class.这将影响您班级中的所有表。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM