Custom TableCellRenderer/TreeTableCellRenderer doesn't render Table cells

前端 未结 1 1122
长情又很酷
长情又很酷 2020-12-10 17:59

I made this CustomCellRenderer class intended to be used in JXTreeTable and JXTable objects since I have many of these in my project.

相关标签:
1条回答
  • 2020-12-10 18:46

    Thanks everybody for your comments and suggestions. I found the solution in JComponent#setBackground(Color bg) documentation:

    Sets the background color of this component. The background color is used only if the component is opaque, and only by subclasses of JComponent or ComponentUI implementations. Direct subclasses of JComponent must override paintComponent to honor this property.

    Since my CustomCellRenderer extends from JLabel the only thing I have to do is make sure it's opaque and its background color will be painted:

    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
        setOpaque(true);//adding this line I solved my problem
        setBackground(isSelected ? new Color(83,142,213) : Color.white);
        setForeground(isSelected ? Color.white : Color.black);
        setText(value != null ? value.toString() : "<null>");
        return this;
    }
    

    enter image description here

    0 讨论(0)
提交回复
热议问题