编程语言
首页 > 编程语言> > 如何编写自定义DefaultTableCellRenderer来着色特定单元格并“保留”其他单元格的颜色,Java

如何编写自定义DefaultTableCellRenderer来着色特定单元格并“保留”其他单元格的颜色,Java

作者:互联网

我正在使用netbeans及其gui builder来创建桌面应用程序.
为了在我的jTables中着色特殊单元格,我已经基于示例代码实现了自定义DefaultTableCellRenderer.到目前为止它的作用.

我的问题是覆盖函数“getTableCellRendererComponent()”的else情况.我不希望不在指定范围内的单元格变成“白色” – 我希望这些单元格以它们已有的颜色显示(例如,如果它们有,我不想将那些颜色更改为“白色”一直是“红色”).

如果有人可以帮助我,我会很高兴!

非常感谢提前.
斯特芬

class ColoredTableCellRenderer extends DefaultTableCellRenderer
{

    /** constructor */
    public ColoredTableCellRenderer(int rowToColorizeStart, int rowToColorizeStop, int columnToColorizeStart, int columnToColorizeStop, Color color)
    {
        mRowToColorizeStart = rowToColorizeStart;
        mRowToColorizeStop = rowToColorizeStop;
        mColumnToColorizeStart = columnToColorizeStart;
        mColumnToColorizeStop = columnToColorizeStop;
        mColor = color;
    }

    private int mRowToColorizeStart = 0;
    public void setRowToColorizeStart(int rowToColorizeStart) {
        mRowToColorizeStart = rowToColorizeStart;  
    }
    public int getRowToColorizeStart() {
        return mRowToColorizeStart;  
    }

    private int mRowToColorizeStop = 0;
    public void setRowToColorizeStop(int rowToColorizeStop) {
        mRowToColorizeStop = rowToColorizeStop;  
    }
    public int getRowToColorizeStop() {
        return mRowToColorizeStop;  
    }

        private int mColumnToColorizeStart = 0;
    public void setColumnToColorizeStart(int columnToColorizeStart) {
        mColumnToColorizeStart = columnToColorizeStart;  
    }
    public int getColumnToColorizeStart() {
        return mColumnToColorizeStart;  
    }

    private int mColumnToColorizeStop = 0;
    public void setColumnToColorizeStop(int columnToColorizeStop) {
        mColumnToColorizeStop = columnToColorizeStop;  
    }
    public int getColumnToColorizeStop() {
        return mColumnToColorizeStop;  
    }

    private Color mColor = Color.WHITE;
    public void setColor(Color color) {
        mColor = color;  
    }
    public Color getColor() {
        return mColor;  
    }


    @Override
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {

        Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);    

        if( (row >= getRowToColorizeStart()) && (row <= getRowToColorizeStop()) && (column >= getColumnToColorizeStart()) && (column <= getColumnToColorizeStop()) && (isSelected == false) ) {
            cellComponent.setBackground(getColor());    
        } else {
           cellComponent.setBackground(Color.WHITE);
        }

        return cellComponent;

    }
}

解决方法:

How to write a custom DefaultTableCellRenderer to colorize specific cells “only”, Java

代码行

public Component getTableCellRendererComponent(JTable table, Object
value, boolean isSelected, boolean hasFocus, int row, int column) {

谈论什么发生了

>是否选择了单元格(boolean isSelected)
>如果选择了单元格和/或具有Focus也是(boolean hasFocus)
>对于JTable中的行(int行)
>来自JTable的列(int列)

然后你可以改变字体,边框,颜色,图标e.i.

通知—>不要在XxxRenderer中更改对象值,永远不要尝试避免这种情况

编辑

> JTable有两个dimmension只定义了行,看看prepareRenderer
> prepareRenderer被指定用于行着色,my question can help youhere,不要忘记覆盖int modelRow = convertRowIndexToModel(row);,在大多数情况下你可以丢失索引模型v.s.查看,因为可以对JTable进行排序或过滤

标签:java,swing,jtable,background-color,tablecellrenderer
来源: https://codeday.me/bug/20190826/1726155.html