Я выделяю JTable
ячеек на основе проверки. В некоторых условиях я должен принять значение других столбцов. Например, если column2
имеет США, то column3
должно быть только числовым. В качестве другого примера, если col2
- это "США" , а 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]