I ve encountered a strange behavior from JTable (JDK 1.5_22):
After a selection change in the table and under some unknown particular circumstances, the JTable will call the cell renderer with null for the value parameter.
This will eventually lead to a nice Null Pointer Exception on a custom renderer code that is not ready for such a rude call.
这里是有罪的方法(JTable.java, 第5319条):
public Accessible getAccessibleChild(int i) {
if (i < 0 || i >= getAccessibleChildrenCount()) {
return null;
} else {
// children increase across, and then down, for tables
// (arbitrary decision)
int column = getAccessibleColumnAtIndex(i);
int row = getAccessibleRowAtIndex(i);
TableColumn aColumn = getColumnModel().getColumn(column);
TableCellRenderer renderer = aColumn.getCellRenderer();
if (renderer == null) {
Class<?> columnClass = getColumnClass(column);
renderer = getDefaultRenderer(columnClass);
}
Component component = renderer.getTableCellRendererComponent(
JTable.this, null, false, false,
row, column);
return new AccessibleJTableCell(JTable.this, row, column,
getAccessibleIndexAt(row, column));
}
}
这里的重点是错误陈述:
Component component = renderer.getTableCellRendererComponent(
JTable.this, null, false, false,
row, column);
引诱“JTable have AccessibleChild 5334”是令人感兴趣的: 我不单靠自己面对这一特点。 但没有答案。
是否有人知道这一点?