Java JTable Alternate Row Color not working

15,652

Solution 1

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.

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

Solution 2

Solution 3

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.

Solution 4

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

        });

Solution 5

The easiest way is:

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

This will affect all the tables in your class.

Share:
15,652
Iqbal Hossain
Author by

Iqbal Hossain

I am a lazy freelancer. Too much fan in programming. But not stable to any specific programming language. I like to search the answer of "Why" always.

Updated on June 28, 2022

Comments

  • Iqbal Hossain
    Iqbal Hossain about 2 years

    Why this following code not working? where is the problem? My jTable is initiated as 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;
                };
    
            });