我正在尝试创建文件验证器,因此,导入一个csv文件,检查内容,检查每个字段是否为空,然后检查每个字段的长度。如果它大于指定的值(每个字段不同),那么我想将其显示为不同的颜色,以便我可以轻松地识别出不适合我的数据库的任何字段。
我设法将文件读入数组,检查它是否不为null,并对照静态值检查字段长度。我有另一个包含字段大小的数组,但是我似乎无法获得带有valaidateArrayElement 0的交叉引用dataArrayElement 0。
我已经将其输出到JTable,并以其他颜色显示击中“ TOOBIG”的所有内容。
Integer[] validateLength = {8,2,22,11,25,13,6,2,34,11,35,35,34,11,1};
class checkValue{
public String validate(String value){
// Check to see if the cell is null or blank
if (StringUtils.isNullOrBlank(value)) {
return "EMPTY";
} else if (value.length() > 8) { //Work out how to set this as differing field lengths
return "TOOBIG";
}
else {
return "OK";
}
}
}
class CustomRenderer extends DefaultTableCellRenderer
{
private static final long serialVersionUID = 6703872492730589499L;
private checkValue cv = new checkValue();
public JComponent getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
{
JComponent cellComponent = (JComponent) super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);
if (cv.validate(table.getValueAt(row, column).toString()).equals("TOOBIG")){
cellComponent.setBackground(Color.RED);
cellComponent.setForeground(Color.YELLOW);
cellComponent.setToolTipText("Data is too big for the DB");
} else if(cv.validate(table.getValueAt(row, column).toString()).equals("EMPTY")){
cellComponent.setBackground(Color.GRAY);
cellComponent.setForeground(Color.WHITE);
cellComponent.setToolTipText("Field is empty in import file!");
} else {
cellComponent.setBackground(Color.WHITE);
cellComponent.setForeground(Color.BLACK);
}
return cellComponent;
}
}
任何帮助,将不胜感激!
谢谢
您可以在validate方法中传递数据元素的索引,并从validateLength检查相应的值
Integer[] validateLength = {8,2,22,11,25,13,6,2,34,11,35,35,34,11,1};
public String validate(String value, int index){
// Check to see if the cell is null or blank
if (StringUtils.isNullOrBlank(value)) {
return "EMPTY";
} else if (value.length() > validateLength[index]) {
return "TOOBIG";
}
else {
return "OK";
}
}
并且在调用此方法时传递索引(假设要验证的值跨列)
validate(table.getValueAt(row, column).toString(), column);
希望这可以帮助
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句