Search code examples
javajtablerenderer

Table renderer where there are 2 if statements


I have a table renderer that makes a row in my table red depending on the contents of column 11. This works fine and the code is below:

tableR = new JTable(modelR)
{
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);


Font myFont = new Font("Arial",Font.PLAIN,10);
Font myFont1 = new Font("Arial", Font.BOLD,10);
int rowModelId = convertRowIndexToModel( row );


if (!isRowSelected(row)) {
                    if (tableR.getColumnCount() >= 0) {
                   String type = (String) getModel().getValueAt(rowModelId, 11);
                        c.setBackground("0.0".equals(type) ? Color.RED : Color.WHITE);
                        c.setForeground("0.0".equals(type) ? Color.WHITE : Color.BLACK);
                        c.setFont("0.0".equals(type) ? myFont1: myFont);

                    }

 }  

 return c;

}

I now want to additionally implement the same thing with column 12, so that if a criteria is met, in this case "u" that particular row is yellow. My attempt is below however now no colour appears at all in the table. In addition to this what will happen if column 11, and column 12 are coloured --what would happen in this scenario?

Here is my attempted go:

tableR = new JTable(modelR)
{
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component c = super.prepareRenderer(renderer, row, column);


Font myFont = new Font("Arial",Font.PLAIN,10);
Font myFont1 = new Font("Arial", Font.BOLD,10);
int rowModelId = convertRowIndexToModel( row );
int rowModelId1 = convertRowIndexToModel( row );

if (!isRowSelected(row)) {
                    if (tableR.getColumnCount() >= 0) {
                              String type = (String) getModel().getValueAt(rowModelId, 11);
                        c.setBackground("0.0".equals(type) ? Color.RED : Color.WHITE);
                        c.setForeground("0.0".equals(type) ? Color.WHITE : Color.BLACK);
                        c.setFont("0.0".equals(type) ? myFont1: myFont);

                    }
                         if (tableR.getColumnCount() >= 0) {
                              String type = (String) getModel().getValueAt(rowModelId1, 12);
                        c.setBackground("u".equals(type) ? Color.YELLOW : Color.WHITE);
                        c.setForeground("u".equals(type) ? Color.WHITE : Color.BLACK);
                        c.setFont("u".equals(type) ? myFont1: myFont);

                    }


 }  


 return c;

}

Solution

  • tableR = new JTable(modelR) {
        @Override
        public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
            Component c = super.prepareRenderer(renderer, row, column);
            Font myFont = new Font("Arial",Font.PLAIN,10);
            Font myFont1 = new Font("Arial", Font.BOLD,10);
            int rowModelId = convertRowIndexToModel( row );
            int rowModelId1 = convertRowIndexToModel( row );
        if (!isRowSelected(row)) {
            if (tableR.getColumnCount() >= 0) {
                String type = (String) getModel().getValueAt(rowModelId1, 12);
                if("u".equals(type)) {
                    c.setBackground(Color.YELLOW);
                    c.setForeground(Color.WHITE);
                    c.setFont(myFont1);
                    return c;
                }
                type = (String) getModel().getValueAt(rowModelId, 11);
                if("0.0".equals(type)) {
                    c.setBackground(Color.RED);
                    c.setForeground(Color.WHITE);
                    c.setFont(myFont1);
                    return c;
                }
            }
            c.setBackground(Color.WHITE);
            c.setForeground(Color.BLACK);
            c.setFont(myFont);
        }
        return c;
    }
    }
    

    here you go, i hope this solves it