2013-07-29 47 views
1

我想我在這裏有一個奇怪的問題。這裏是我的代碼:Java swing setSelectionForeground性能問題

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%左右。我覺得這很奇怪,不知道這裏出了什麼問題。 我希望你能幫助我。

+2

看來'DefaultTableCellRenderer'沒有'setSelectionForeground',我認爲這是一個類似於你已經實現的'JTable'類的內部類,然後每次你調用'setSelectionForeground ',表格正試圖重新渲染任何可能被選中的單元格,以及觸發屬性更改事件,並且通常陷入令人討厭的更新循環中...... – MadProgrammer

+0

這實際上是JTable的內部類。那麼你的意思是說setSelectionForeground是JTable的一種方法而不是渲染器? – lugte098

+0

這是正確的。嘗試在單元格渲染器的上下文中輸入'this.setSelectionForeground(Color.BLUE)',看看它是否會編譯。 – MadProgrammer

回答

6

調用setSelectionForeground()時間表調用repaint()的四倍時間它被稱爲爲細胞每。相反,

  • 不要在渲染器中調用setSelectionForeground()

  • 條件顏色一次基於值isSelected

+0

所以我怎麼去關於設置一次?我有點新搖擺。 – lugte098

+0

@ lugte098他們的方式,你現在應該工作得很好(即'setForgroud'等) – MadProgrammer

+0

是的,它工作正常,但是當我選擇一行時它又變成了黑色。因此,對於選定的項目,我需要其他東西 – lugte098

2

使用

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

  2. getValueXxxTableModel嚴格convertViewToModel(例如JTables視圖可以被排序或過濾)

  3. 作業爲prepareRenderer

+0

我真的不明白你在說什麼atm – lugte098

+2

+1對於'convertXxx()',這很容易被忽略,直到有人移動一列。 – trashgod

+1

@ lugte098 [查看和取消註釋代碼](http://stackoverflow.com/q/7132400/714968)XxxRenderers的其餘部分相同 – mKorbel