2012-03-21 84 views
2

我需要幫助。在Jtable的一行中設置顏色

我有兩個表。 enter image description here

在指令表中,每行必須根據在流水線階段執行的指令來突出顯示。比如說,在t10時刻,I5處於IS階段,所以指令表中的I5必須高亮顯示,否則指令表中的行顏色必須改變。I5行爲紅色,I6行爲顏色粉紅色, I7是顏色綠色,I8是顏色灰色,I9是顏色橙色。

我真的需要你的專業知識,謝謝.. :)

+3

你有沒有通過[JTable教程](http://docs.oracle.com/javase/tutorial/uiswing/components/table.html)?這類事情在那裏很好解釋。請檢查一下,特別是關於創建自定義渲染器的部分。聽起來好像你會想要閱讀[SwingWorker教程](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/index.html),因爲你的「指令執行」可能需要在後臺線程上完成。 – 2012-03-21 13:25:59

+2

有可能是使用外觀和感覺的問題,這是純粹的Nimbus或基於Nimbus的自定義L&F ... – mKorbel 2012-03-21 13:28:49

+0

啊,我希望我們的居民JTable渲染專家會出現,他有! – 2012-03-21 13:31:28

回答

3

請嘗試這種使用自定義的渲染,這將解決你的問題很容易

JTable myTable = new JTable(); 
// You can specify the columns you need to do the required action 
myTable.getColumnModel().getColumn(0).setCellRenderer(new MyRenderer()); 

public class MyRenderer extends DefaultTableCellRenderer { 

    // This is a overridden function which gets executed for each action to 
    /// your Jtable 
    public Component getTableCellRendererComponent (JTable table, 
     Object obj, boolean isSelected, boolean hasFocus, int row, int column) { 

     // Use this row, column to change color for the row you need, e.g. 
     if (isSelected) { // Cell selected 
      cell.setBackground(Color.green); 
     } 
    } 
} 

注:此渲染器可以使用超過做顏色突出顯示,請參考custom Jtable rendering。爲了響應隊列計時您的更改,您可以將其安排在單獨的線程中。

+3

請學習java命名約定並堅持使用它們 – kleopatra 2012-03-21 13:42:40