3

I'm using netbeans with its gui builder in order to create a desktop application. To colorize special cells in my jTables I have implemented a custom DefaultTableCellRenderer based on example code. So far it works.

My problem is the else case of the overwritten function "getTableCellRendererComponent()". I don't want that a cell which is not in the specified range becomes "white" - I want that these cells appear in the color they already had (e. g. I don't want to change there color to "white" if they have been "red").

I would be happy if somebody could help me on that!!

Thanks a lot in advance.
Steffen

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;

    }
}
macktheknife
  • 41
  • 1
  • 4

3 Answers3

4

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

code line

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

talking about whats happens

  • if cell is selected or not (boolean isSelected)

  • if cell is selected and /or has Focus too (boolean hasFocus)

  • for row in the JTable (int row)

  • in the Column from JTable (int column)

then there you can to change Font, Border, Color, Icon e.i.

NOTICE ---> don't change Object value in the XxxRenderer, never ever, be sure to try to avoid that

EDIT

  • JTable has two dimmension there are defined only rows, have look at prepareRenderer

  • prepareRenderer is designated for row coloring, my question can help you or here, don't forget to override int modelRow = convertRowIndexToModel(row);, in most cases you can lost index model v.s. view, because JTable can be sorted or filtered

Community
  • 1
  • 1
mKorbel
  • 109,525
  • 20
  • 134
  • 319
2

I believe what you are looking for is the following. It uses the table's fg and bg for painting the cell.

else {
    setBackground(table.getBackground());
    setForeground(table.getForeground());
}
Devon_C_Miller
  • 16,248
  • 3
  • 45
  • 71
0

You should change your else block to

 else if (cellComponent.getBackground != color){
         cellComponent.setBackground(white);
  }
Joseph Elcid
  • 887
  • 1
  • 6
  • 21
  • I changed to:`else if (cellComponent.getBackground() != getColor()) { cellComponent.setBackground(Color.WHITE); }` But the whole table get's the special color now (all cells). – macktheknife Oct 15 '12 at 12:28