Question

I think I have a weird problem here. Here is my code:

private class BorderCellRenderer extends DefaultTableCellRenderer {
    private Border extraBorder;
    /**
     * A cell render is based on labels which are changed to display the correct appearance
     * This cell renderer adds extra border to every render action
     */
    BorderCellRenderer(Border extraBorder) {
        this.extraBorder = extraBorder;
    }


    /**
     * The setBorder() is used to change the cell appearance.
     * The trick is to override the call (of JComponent) and to add extra space
     * to it by making it an compound border with.
     *
     * Remember that getBorder() now returns our compound border
     */
    public void setBorder(Border border) {
        super.setBorder(new CompoundBorder(border, extraBorder));
    }

    public Component getTableCellRendererComponent(JTable table, Object value,
                                                    boolean isSelected, boolean hasFocus,
                                                    int row, int column) {
        setFont(table.getFont());
        setText(value.toString());

        /* set color depending on the state of the row */
        TableRowItem rowItem = ((FooModel) table.getModel()).getRow(row);

        String state = rowItem.getState();

        /* set tool tip on EMPLOYEE column */
        if(column == FooModel.COL_EMPLOYEE) {
            setToolTipText("blalba");
        }

        /* Paint text according to state*/
        if(state.equals(RowItemState.ENTERED)) {
            setForeground(Color.black);
            //TODO setSelectionForeground(Color.black);
        }
        if(state.equals(RowItemState.SUBMITTED)) {
            setForeground(Color.blue);
            //TODO setSelectionForeground(Color.blue);
        }
        if(state.equals(RowItemState.APPROVED)) {
            Color green = new Color(0, 128, 0); // greenish
            setForeground(green);
            //TODO setSelectionForeground(green);
        }
        if(state.equals(RowItemState.BOOKED)) {
            setForeground(Color.red);
            //TODO setSelectionForeground(Color.red);
        }

            switch (column) {
            case FooModel.COL_EMPLOYEE:
            case FooModel.COL_SAT:
            case FooModel.COL_SUN:
                if(state.equals(RowItemState.CHECKED)) {
                    setBackground(new Color(183, 244,176)); //light green
                } else {
                    setBackground(java.awt.Color.lightGray);
                }
                break;
            default:
                if(state.equals(RowItemState.CHECKED)) {
                    setBackground(new Color(183, 244,176)); //light green
                } else {
                    setBackground(java.awt.Color.white);
                }
                break;
            }
       //}

        if (column == FooModel.COL_TOTAL){
            if (table.getSelectedRowCount() > 0){
                int rowsSelected[] = table.getSelectedRows();
                double total = 0;
                for (int j = 0; j < table.getSelectedRowCount(); j++){
                    Double dTemp = (Double) table.getValueAt(rowsSelected[j], FooModel.COL_TOTAL);
                    total += dTemp.doubleValue();
                }
                setToolTipText("Total Selected = " + String.valueOf(total));
            }
        }

        // return super method for selections
        return super.getTableCellRendererComponent(table, value,
                                                    isSelected, hasFocus,
                                                    row, column);
    }
}

Now we get to the fun part. This code example runs fine, however when I uncomment the TODO lines so that the setSelectionForeground() methods are called, my CPU gets to like 20% on a Quad-Core. This happens on all my colleagues PC's as well (also Quad-Cores). When the lines are commented, CPU is around 0-1%. I find this very strange and cannot figure out what is going wrong here. I hope you can help me.

Was it helpful?

Solution

Invoking setSelectionForeground() schedules a call to repaint() each of the four times time it is called for every cell. Instead,

  • Do not call setSelectionForeground() in the renderer.

  • Condition the color once based on the value of isSelected.

OTHER TIPS

use

  1. and to test coordinates boolean isSelected, boolean hasFocus, int row, int column (isSelected/hasFocus)

  2. getValue from XxxTableModel by strictly convertViewToModel (e.g. JTables view can be sorted or filtered)

  3. job for prepareRenderer

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top