编辑jtable的表格中的数据,原本是int类型的(如13),手动编辑后(如变成16),此时变成String类型了。
jtable.getValueAt()函数返回的是Object子类,如开始的13返回的是Integer类型,而之后手动修改后返回的是String类型。此时若要对提取出来的数据进行加减运算,则必然牵扯到String类型转换成int的问题。
当然,如果我们事先知道那些数据都是String类型,那也好办,直接用:
Integer.parseInt(str)但如果之前不知道,要怎么办呢? 
如图,黄色为手动修改后变成16的cell,此时用getValueAt获取三个cell的值,并用getClass获得其类别,得:
class java.lang.String
class java.lang.Integer
class java.lang.Integer这里如果还是用
Integer.parseInt((String)jtable.getValueAt()则会报错: java.lang.Integer cannot be cast to java.lang.String原因是后面两个cell值的类型是Integer,不能强制转换成String
解决办法是用:
Integer.parseInt(jtable.getValueAt(xx,xx).toString()
版权声明:本文为Stark_JC原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。