我已经开发了一个显示JTable的swing应用程序。我希望当用户修改单元格值时,修改后的单元格更改颜色。
这是用户修改单元格时运行的代码:
this.myTable.getColumnModel().getColumn(column).setCellRenderer(new StatusColumnCellRenderer());
这是我的单元格渲染类的代码:
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.
TableModelLotti tableModel = (TableModelLotti) table.getModel();
if(isSelected)
l.setBackground(Color.GREEN);
//Return the JLabel which renders the cell.
return l;
}
}
万千封印
相关分类