1

ここに奇妙な問題があると思います。これが私のコードです:

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

楽しい部分に移ります。このコード例は正常に実行されますが、メソッドが呼び出されるように TODO 行のコメントを外すsetSelectionForeground()と、クアッドコアで CPU が 20% 程度になります。これは、同僚のすべての PC でも発生します (クアッドコアも同様です)。行がコメントされている場合、CPU は約 0 ~ 1% です。これは非常に奇妙で、何が問題なのかわかりません。あなたが私を助けてくれることを願っています。

4

2 に答える 2

6

呼び出しは、セルごとに呼び出される 4 回の時間ごとsetSelectionForeground()に呼び出しをスケジュールします。その代わり、repaint()

  • setSelectionForeground()レンダラーを呼び出さないでください。

  • の値に基づいて色を一度調整しますisSelected

于 2013-07-29T08:07:55.303 に答える