2012-03-21 69 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