如何检索JTable中的数据为一个数组 [英] How to Retrieve JTable Data as an Array
问题描述
我已经通过与(一个DefaultTableModel填充一个JTable对象[] []
的数据,的String []
标题)构造函数。用户可以编辑的表,我希望能够回到加载新的数据到一个数组(对象[] []
)。请注意,我宁愿不只是更新由位阵列位,但能够完全只是从表中加载新的数组。如何才能做到这一点?
I've populated a JTable through a DefaultTableModel with the (Object[][]
data, String[]
headers) constructor. Users can edit the table, and I want to be able to load the new data back into an array (Object[][]
). Note that I'd rather not just update the array bit by bit, but be able to just completely load a new array from the table. How can this be done?
推荐答案
我收回那句话,2日的想法,你不需要任何类型转换 - TableModel的是,有所有3方法调用你需要的接口。 :)
I take that back, on 2nd thoughts, you dont need any typecasting - TableModel is an interface that has all the 3 method calls you need. :)
摘要:获取表的模型,检查其类并将其类型强制转换为相应的类(abstract或默认的TableModel),以及使用它的方法来加载新创建的数组。有些伪code:
Summary: Get the model for the table, check its class and typecast it to appropriate class (Abstract or Default TableModel), and use its methods to load a newly created array. Some psuedoCode:
public Object[][] getTableData (JTable table) {
DefaultTableModel dtm = (DefaultTableModel) table.getModel();
int nRow = dtm.getRowCount(), nCol = dtm.getColumnCount();
Object[][] tableData = new Object[nRow][nCol];
for (int i = 0 ; i < nRow ; i++)
for (int j = 0 ; j < nCol ; j++)
tableData[i][j] = dtm.getValueAt(i,j);
return tableData;
}
您的头不应该由用户编辑修改。希望有所帮助。问候, - M.S
Your headers should not have changed by user-edits. Hope that helps. Regards, - M.S.
这篇关于如何检索JTable中的数据为一个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!