从JTable中获取Integer的价值? [英] Getting value from JTable as Integer?

查看:84
本文介绍了从JTable中获取Integer的价值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不明白.我正在使用DefaultTableModel,而我的尝试是将此值作为int放在表格中:

I don't get it; I'm using the DefaultTableModel, and my attempt was this to get the value in a tabel as an int:

Integer.parseInt( tableModel.getValueAt(i, 1) );

Eclipse表示需要从ObjectString的转换,因此eclipse可以做到:

Eclipse is saying there is a cast needed from Object to String, so eclipse makes that:

Integer.parseInt( (String) tableModel.getValueAt(i, 1) );

在运行时,由于无法将"int转换为字符串"而导致程序崩溃.为什么?我期望对象->字符串->整数".

At runtime, the program crashes because it is not possible to cast "int to string". Why? I expected "object -> string -> int".

推荐答案

尝试使用:

Integer.parseInt( tableModel.getValueAt(i, 1).toString() );

只需使用toString()设置对象的字符串表示形式即可.

Just set string representation of the object by using toString().

这篇关于从JTable中获取Integer的价值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆