Custom TableCellRenderer/TreeTableCellRenderer doesn't render Table cells

六眼飞鱼酱① 提交于 2019-11-28 11:30:28

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!