jTable Cell background color

后端 未结 3 1203
独厮守ぢ
独厮守ぢ 2021-01-24 21:02

I am trying to color a cell of a jTable using the renderers, but they are not working well, as they lag the table and make it impossible to see. here\'s my code:



        
3条回答
  •  不要未来只要你来
    2021-01-24 21:35

    ok, after looking in the discussion thread, i come to this solution...

    ...don't try to get the rendered Component itself - tell the renderer to draw your desired component in ways you want it to...

    CellRenderer renderer = new DefaultCellRenderer(){
    
        public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
    
            Component c = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
            JLabel label = (JLabel)c;
    
            if (yourAlgorithmToDetectTheProperCell){ //i can't insert your condition from above, it's overkill ^^
                label.setBackGround(Color.RED);
            }
    
            return label;
        }
    };
    
    table.setCellRenderer(renderer);
    

    SDF

提交回复
热议问题