2016-01-26 18 views
0

我已經我的方法中的以下渲染器和工具提示渲染器,行不再強調

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

這得到其中每10秒發生的方法的每個刷新之後施加。

我也有這個表的過濾器監聽方法採取以下形式:

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


    } } 

    ); 
    } 

但是在我的過濾表中的渲染器不再突顯紅色..ie右行的那些都是0.0

我該如何處理?我需要刪除渲染器,然後重新應用它嗎? 我是否需要將我的渲染器方法合併到過濾器方法中..幫助表示讚賞。

回答

2

渲染器報告視圖索引,而不是模型索引。您使用的視圖索引指數模型在聲明中String type = (String) getModel().getValueAt(row, 11);

if語句,你設置的顏色應爲:

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

你也可以寫它使用GET單元格的值JTable.getValueAt(該方法花費視圖的索引),以避免不必使用convertRowIndexToModel方法如下:

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

@ TT渲染器工作正常然而問題是,後我過濾表作爲文檔偵聽,高亮無羅nger突出顯示正確的行,因爲表現在行數較少。我需要突出顯示新過濾表 – Ingram

+0

@MrAssistance讓我先問一下,你是否同時應用了我所回答的兩個版本,並且在這兩種情況下你是否看到相同的行爲? –

+0

@ TT我沒有沒有,因爲我不認爲問題出在這裏 - 問題是,我輸入過濾器後,它過濾表模型,然後突出顯示的行不再突出顯示正確的行。我想是因爲當前桌面上的亮點固定.. – Ingram