简体   繁体   English

如何将值设置为整数?

[英]How do i set the value to be integer?

is it possible for the cell to be input with integer only? 单元格只能用整数输入吗? This is my code. 这是我的代码。

@Override
public boolean isCellEditable(int row, int col) {
    return col == 3;
}

@Override
public void setValueAt(Object value, int row, int col) {
    data[row][col] = value;
    fireTableCellUpdated(row, col);
}

Yes. 是。 Use instanceof to check if object is of type Integer. 使用instanceof检查对象是否为Integer类型。 ( Usage Example ) 用法示例

If String is going to be sent and you want to check if it is integer, use 如果要发送字符串,并且您要检查它是否为整数,请使用

Integer.parseInt((String)value) 的Integer.parseInt((字符串)值)

It throws an exception when the value is not parsable or value is not of type String. 当值不可解析或值类型不是String时,它将引发异常。 You can implement your control accordingly. 您可以相应地实现控制。

Simple solution: 简单的解决方案:

@Override
public void setValueAt(Object value, int row, int col){

    if(!isIntegerValue(value)) {
        throw new Exceoption("not integer value");
    }

    data[row][col]=value;
    fireTableCellUpdated(row, col);
}

private boolean isIntegerValue(Object value) {
    return value instanceof Integer;
}

Check the type of value ( value instanceof Integer ) and throw an Exception in case it is not integer. 检查value的类型( value instanceof Integer ),如果它不是整数,则抛出Exception。 Unless you do not want to use some kind of wrapper, there is no chance to ensure the type of value at compile time, while you have to keep the method's header. 除非您不想使用某种包装器,否则没有机会在编译时确保value的类型,而必须保留方法的标头。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM