puuuuuuf,我开始喜欢摇摆 :) 我正在尝试编写一个 cellRenderer 来自定义渲染除第一行和第一列中的所有单元格之外的所有单元格。所以我写了以下内容:
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
public Component getTableCellRendererComponent(JTable table, Object obj, boolean isSelected, boolean hasFocus, int row, int column) {
Component cell = super.getTableCellRendererComponent(table, obj, isSelected, hasFocus, row, column);
if(row >0&&column>0){
cell.setBackground(Color.GREEN);
}
return cell;
}
}
并将渲染器设置如下:
scheduleTable.setDefaultRenderer(Object.class, new CustomTableCellRenderer());
但由于某种原因,这种方法将渲染器应用于所有单元格。所以他们都是绿色的。如果我做错了什么,你能帮我吗?
提前致谢!
添加
scheduleTable = new JTable() {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component comp = super.prepareRenderer(renderer, row, column);
int modelRow = convertRowIndexToModel(row);
int modelColumn = convertColumnIndexToModel(column);
if (modelColumn != 0 && modelRow != 0) {
comp.setBackground(Color.GREEN);
}
return comp;
}
};
这段代码也使所有表格都变绿了。
这段代码:
scheduleTable = new JTable(tableModel) {
@Override
public Component prepareRenderer(TableCellRenderer renderer, int row, int column) {
Component comp = super.prepareRenderer(renderer, row, column);
int modelRow = convertRowIndexToModel(row);
int modelColumn = convertColumnIndexToModel(column);
if (modelRow != 0 && modelColumn != 0) {
setBackground(Color.GREEN);
} else {
setBackground(Color.WHITE);
}
return comp;
}
};
给我以下结果;(
以下情况我有不同n
的表达方式row != 0 && column != 0
: