我JTable
根据验证突出显示单元格。在某些情况下,我必须取其他列的值。例如,如果column2
有 USA 那么column3
应该只是数字。另一个例子,如果col2
是“USA”并且 col4
是数字,那么col5
应该只有三个字符。有人可以建议如何做到这一点吗?
在下面的片段中,col3
包含国家名称; col4
并col5
依赖col3
. 当我在case 3
和 中时case 4
,我无法检查 的值case 2
。例如,我想要喜欢,if (col3.value == "USA")
。
[code]
tcol = editorTable.getColumnModel().getColumn(0);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = editorTable.getColumnModel().getColumn(1);
tcol.setCellRenderer(new CustomTableCellRenderer());
tcol = editorTable.getColumnModel().getColumn(2);
tcol.setCellRenderer(new CustomTableCellRenderer());
public class CustomTableCellRenderer extends DefaultTableCellRenderer {
@Override
public Component getTableCellRendererComponent (JTable table, Object
value,boolean isSelected, boolean hasFocus, int row, int col){
Component cell = super.getTableCellRendererComponent(table, value,
isSelected,hasFocus, row, col);
if (value instanceof String) {
String str = (String) value;
switch (col) {
case 0:
col1(str, cell);
break;
case 1:
col2(str, cell);
break;
case 2:
col3(str, cell);
break;
}
}
return cell;
}
private void col1(String str, Component cell) {
if(!str.matches("[0-9a-zA-z]")){
cell.setBackground(Color.RED);
} else {
cell.setBackground(Color.GREEN);
}
}
private void col2(String str, Component cell) {
if(!str.matches("[A-Z]{3}")){
cell.setBackground(Color.RED);
} else {
cell.setBackground(Color.GREEN);
}
}
[/code]