我正在尝试实现一个按钮,当单击它时,该行中单元格的颜色会改变颜色。我有一个cellRenderer类:
public class MyCellRenderer extends javax.swing.table.DefaultTableCellRenderer {
public java.awt.Component getTableCellRendererComponent(javax.swing.JTable table, java.lang.Object value, boolean isSelected, boolean hasFocus, int row, int column) {
java.awt.Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
cellComponent.setBackground(java.awt.Color.RED);
return cellComponent;
}
}
按如下方式调用:(对于第15列,让我们说):
MyCellRenderer mcr = new MyCellRenderer();
Mytable.getColumnModel().getColumn(15).setCellRenderer(mcr);
注意它是如何在ColumnModel对象上调用的。是否有方法选择单元格(行,列)并根据坐标更改其颜色。这样它可以被JTable对象调用吗?例如:
Mytable.colorCell(1,7); //colors cell in row 1, column 7
谢谢