我想更改 JTable 中整行的颜色。
我定义了 JTable:
JTable table = new JTable(data, columnNames);
其中 data, columnNames 是字符串表。
最常见的方法是编写自己的类:
public class StatusColumnCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int col) {
//Cells are by default rendered as a JLabel.
JLabel l = (JLabel) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, col);
//Get the status for the current row.
l.setBackground(Color.GREEN);
//Return the JLabel which renders the cell.
return l;
}
}
并致电:
this.table.getColumnModel().getColumn(0).setCellRenderer(new StatusColumnCellRenderer());
但它不起作用。我究竟做错了什么?