2015-05-15 34 views
1

嗯,這是我第一篇文章。但我有點沮喪,因爲我到處搜索,但沒有任何工作。我有JTable和代碼正常工作後如果(value.equals("CMAU1294522"))右下方行。但只有一個單元格是顯示方形框。我點擊了一個特定的行,我希望整行顯示淺灰色(我認爲這是標準)。JTable中的選定行不突出顯示

table = new JTable(sorter) { 
    public Component prepareRenderer(TableCellRenderer renderer, int row, int col) { 
     Component comp = super.prepareRenderer(renderer, row, col); 
     Object value = getModel().getValueAt(row, col); 


     UIManager.put("Table.editRow", new javax.swing.plaf.ColorUIResource(Color.YELLOW)); 


     if(editingRow ==1){ 
      table.setSelectionBackground(Color.red); 
     } 
     if (getSelectedRow() == row) { 
      table.setSelectionBackground(Color.red); 
     } 
     if (value.equals("CMAU1294522")) { 
      comp.setBackground(Color.red); 
     } else if (value.equals("PNCT")) { 
      comp.setBackground(Color.green); 
     } else if (NewJApplet.contReady.containsKey(value)) { 
      comp.setBackground(Color.CYAN); 
     } else if (NewJApplet.badCont.containsKey(value)) { 
      comp.setBackground(Color.red); 
     } else { 
      comp.setBackground(Color.white); 
     } 
     return comp; 
    } 

有沒有辦法做到這一點在prepareRenderer功能我已經有了?

回答

1

我有沒有辦法在PreparRenerere函數中執行它我已經有了?

你的代碼得到的「值」是錯誤的。你總是得到正在渲染的當前單元格的值。如果你想突出基於特定值的整行,那麼你需要硬編碼列:

Object value = getModel().getValueAt(row, ???); 

此外,它看起來像你在表中對數據進行排序,所以你應該使用getValueAt(.. )而不是getModel()。getValueAt(),所以你檢查排序表中的數據,而不是未排序模型中的數據。

查看Table Row Rendering中的示例代碼以獲取工作示例。

+0

謝謝。但它終於奏效了。我非常感謝你的幫助。 –

+0

@HiTech,很高興幫助,不要忘記「接受」答案,通過點擊複選標記,讓人們知道問題已經解決。 – camickr