如何访问JTable中每个单元格的数据 [英] How to access the data of each cell in a JTable
问题描述
我已经使用教程来了解如何实现JTable,但是我不知道如何精确地访问每个单元格的数据以提取用户放入其中的数据.
I have used a tutorial to see how to implement JTable but I don't know how exactly access the data of each cell to extract the data that the user put in these.
该表具有 2 列和 N 行
第一列中有一个 String ,第二列中有一个 int
In the first column there is a String in the second there is a int
我使用的教程是此
推荐答案
每个JTable
都有一个与其关联的数据模型.用户可以将数据添加到该数据模型中(例如,通过调用javax.swing.table.TableModel.setValueAt(Object, int, int)
方法),然后JTable
显示它们.为了处理JTable
中的数据,可以使用以下方法:
Every JTable
has a data-model connected with it. Users may add data to this data model (e.g. by calling to the javax.swing.table.TableModel.setValueAt(Object, int, int)
method) and JTable
then displays them. In order to process the data from the JTable
one can use the following approach:
JTable t = new JTable(/* set some table-model that will contain the data */);
...
/* get some table-model that will contain the data */
TableModel tm = t.getModel();
for (int i = 0; i < tm.getRowCount(); i++) {
for (int j = 0; j < tm.getColumnCount(); j++) {
Object o = tm.getValueAt(i, j);
if (o instanceof Integer) {
System.out.println((Integer)o);
} else if (o instanceof String) {
System.out.println((String)o);
}
}
}
这篇关于如何访问JTable中每个单元格的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!