Validating and Highlighting Jtable Cell
I am highlighting jtable cell based on validation. In some conditions, i have to take the value of other columns, for example, if column2 has USA then column3 should be only numeric. another example, if col2 is "USA" AND col4 is numeric then col5 should be only 3 chars. Can some one point how this can be done?
col3 contains country names. col4 and col5 depends on col3. when i am in case 3 and in case 4, i cannot check the value of case 2. example, i want like, if(col3.value == "USA")
I posted this question in Stack Overflow and got only 2 responses. It would be very helpful if i get a solution to the problem with the present code itself with small alterations. Thanks
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]
Re: Validating and Highlighting Jtable Cell
Moved from Advanced Java.
Use JTable#getValueAt(row, 0/1/2)
db
Re: Validating and Highlighting Jtable Cell
Excellent, that's what i was looking for.
One more thing is, should i have to convert, row/column coordinates area view?
Re: Validating and Highlighting Jtable Cell
The row/column received by the renderer's method are the view coordinates. If you are querying the values from the model then yes, you need to convert from view to model coordinates.
db