Search code examples
javaswingjtabletablecellrendererrowfilter

Renderer after a filter, rows no longer highlighted


I have the following renderer and tooltip within my method

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);
        if (!isRowSelected(row)) {
            if (tableR.getColumnCount() >= 0) {
                String type = (String) getModel().getValueAt(row, 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;
    }

    @Override
    public String getToolTipText(MouseEvent e) {
        String tip = null;
        java.awt.Point p = e.getPoint();
        int rowIndex = rowAtPoint(p);
        int colIndex = columnAtPoint(p);
        int realColumnIndex = convertColumnIndexToModel(colIndex);

        if (realColumnIndex != 20) { //Sport column
            tip = " " + getValueAt(rowIndex, colIndex);
            //tip = super.getToolTipText(e);
        }
        return tip;
    }
};

This gets applied after each refresh of the method which occurs every 10 seconds.

I also have a filter listener method on the table this takes the form:

 private void filter2method() {

    filterR.getDocument().addDocumentListener(new DocumentListener() {

    @Override
    public void insertUpdate(DocumentEvent e) {
newFilter();
    }

    @Override
    public void removeUpdate(DocumentEvent e) {
    newFilter();
    }

    @Override
    public void changedUpdate(DocumentEvent e) {
newFilter();
    }

    private void newFilter() {
    RowFilter <DefaultTableModel, Object>rf =null;
   try {
        rf = RowFilter.regexFilter(filterR.getText(),2);
    } catch (java.util.regex.PatternSyntaxException e) {
        return;
    }
    sorter.setRowFilter(rf);


    } }

    );
    }

However upon filtering my table the renderer no longer highlights the right rows in red ..ie the ones that are 0.0

How do I deal with this? Do I need to remove the renderer and then reapply it? Do I need to incorporate my renderer method into the filter method.. help appreciated.


Solution

  • The renderer reports view-indices, not model-indices. You are using a view-index to index the model in the statement String type = (String) getModel().getValueAt(row, 11);

    The if-statement where you set the colors should read:

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

    You could also have written it to get the cell value using JTable.getValueAt (this method takes view-indexes) to avoid having to use the convertRowIndexToModel method as follows:

    if (!isRowSelected(row)) {
        if (tableR.getColumnCount() >= 0) {
            String type = (String) getValueAt(row, 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);
        }
    }