1

我想同时为特定的单元格着色jtable

例如,具有column = 1row i+2(i from 0 to 5 ) 的单元格。

CustomTableCellRenderer我使用示例显示成功地为特定单元格着色

public class CustomTableCellRenderer extends DefaultTableCellRenderer 
{  
   int x;
   int y;

    CustomTableCellRenderer(int x,int y){ //constructor

        this.x= x;
        this.y=y;
    }   

    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected,boolean hasFocus, int row, int column) 
    {
        Component cell = super.getTableCellRendererComponent
           (table, value, isSelected, hasFocus, row, column);

        if ( ((row == x) && (column == y))) { //test of equivalence of x and y as parameter
                cell.setBackground(Color.green);
            }

            else {
                cell.setBackground(Color.WHITE);
            }
        return cell;
    }
}
4

1 回答 1

0
Try this

table.setDefaultRenderer(Object.class, new TableCellRenderer(){
            private DefaultTableCellRenderer DEFAULT_RENDERER =  new DefaultTableCellRenderer();
            private Component comp;

            @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(isSelected){

                    c.setBackground(Color.YELLOW);
                }else{
                if (row%2 == 0){
                    c.setBackground(Color.WHITE);

                }
                else {
                    c.setBackground(Color.LIGHT_GRAY);
                }     }

                return c;
            }

        });
于 2013-12-23T06:16:03.607 回答