混迹Java很多年,然而在碰到JTABLE操作的时候,还是有点望而生畏。
这次在项目中,碰到对CheckBox单元格进行背景色渲染,着实让我煞费
了一番心思。以下是解决方法,与需要的同学共享。
for (int i = 0; i < table.getColumnCount(); i++) {
// 第十二列是预警标志位,用CheckBox控件显示
if (i == 11) {
table.getColumn(table.getModel().getColumnName(i)).setCellRenderer(
new TableCellRenderer() {
private JCheckBox checkBox = new JCheckBox();
@Override
public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column) {
if (value != null) {

本文介绍了在Java编程中如何针对JTable中的CheckBox单元格进行背景色渲染。通过自定义TableCellRenderer,实现了当选中或根据特定条件时,CheckBox背景色变为红色或白色。详细代码示例供读者参考。
最低0.47元/天 解锁文章
1571

被折叠的 条评论
为什么被折叠?



