开发者

TableCellRenderer selected cell problem

开发者 https://www.devze.com 2023-02-13 14:29 出处:网络
I want to implement a tablecellrenderer of a JTable component, which should show a different color depending on the cell data. I got this, but I can\'t change the color of the selected cell. I tried t

I want to implement a tablecellrenderer of a JTable component, which should show a different color depending on the cell data. I got this, but I can't change the color of the selected cell. I tried to do this:

public Component getTableCellRendererComponent(JTable table, Object value,
        boolean isSelected, boolean hasFocus, int rowIndex, int vColIndex)
{

    if (isSelected) {
        this.setBackground((Color)UIManager.get("Table.selectionBackground"));
        this.setBorder(javax.swing.BorderFactory.createLineBorder(new java.awt.Color(0, 0, 0)));
    } else {
        this.setForeground((Color)UIManager.get("Ta开发者_JS百科ble.foreground"));  
        this.setBackground((Color)UIManager.get("Table.background"));  
        this.setBorder(BorderFactory.createEmptyBorder()); 
    } 
...
}

but it does not work :S .. I can not see the problem because the JTable does not show anything different when I click on a cell.


I want to implement a tablecellrenderer of a JTable component, which should show a different color depending on the cell data

The code you posted does not do this. Basically all your code does is duplicate the default behaviour of the renderer

You may find the Table Row Rendering approach easier to implement.


Assuming you're using a JLabel as the base of the component, setting the background will have no effect unless you also set opaque to true. JLabels default to not opaque and so do not paint the background.

0

精彩评论

暂无评论...
验证码 换一张
取 消