Setting color in a row of a Jtable

筅森魡賤 提交于 2020-01-04 06:49:43

问题


I need help.

I have two tables.

In the instruction table., each row must be highlighted according to what instruction is being execute in the pipeline stages. Say for example., at time t10, I5 is in IS stage, so I5 in instruction table must be highlighted or the color of the row in instruction table must be change.say, I5 row is color red, I6 row is color pink, I7 is color green, I8 is color gray, I9 is color orange.

I really need your expertise., thank you.. :)


回答1:


Please try this using custom rendered which will solve your problem easily

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

Note: this renderer can be used for more than doing color highlighting, please refer custom Jtable rendering. For timing your changes in response to the queue, you can schedule it in a separate thread.



来源:https://stackoverflow.com/questions/9805447/setting-color-in-a-row-of-a-jtable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!