2

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

        });
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Iqbal Hossain
  • 97
  • 3
  • 5
  • 16

6 Answers6

22

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));
bbhar
  • 599
  • 5
  • 8
5
Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
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;
        };
    });

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.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138
2

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

        });
Iqbal Hossain
  • 97
  • 3
  • 5
  • 16
2

The easiest way is:

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

This will affect all the tables in your class.

1

Try this one.... 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;
};
Ganesh Rengarajan
  • 2,006
  • 13
  • 26