编辑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()
本文介绍如何处理Java Swing中JTable的手动输入数据导致的数据类型变化问题,特别是从Integer变为String后的处理方法。
1236

被折叠的 条评论
为什么被折叠?



