问题
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