【发布时间】:2014-09-09 01:16:10
【问题描述】:
我必须使用ArrayList 填充JTable,但是当表格显示时,它填充了除正确信息之外的其他内容。您可以点击下一个链接查看我的表格的外观。
gui1 是主类的名称,theObject 是包含要在表格上显示的信息的结构的类的名称。
请帮我解决这个问题!!
ArrayList 定义:
static ArrayList<theObject> datos = new ArrayList<theObject>();
这是用于加载名为“datos”的 ArrayList 的代码:
try { // ------------ carga registros de proyectos --------------- -------------
contador = 0;
fr = new FileReader(file);
br = new BufferedReader(fr);
while ((cadena1 = br.readLine()) != null){
datos1 [0] = cadena1.substring(0,8);
datos1 [1] = cadena1.substring(8,38);
datos1 [2] = cadena1.substring(38,52);
datos1 [3] = cadena1.substring(52,57);
datos1 [4] = cadena1.substring(57,62);
datos1 [5] = cadena1.substring(62,72);
datos1 [6] = cadena1.substring(72,77);
datos1 [7] = cadena1.substring(77,82);
datos1 [8] = cadena1.substring(82,89);
datos1 [9] = cadena1.substring(89,94);
datos1 [10] = cadena1.substring(94,239);
datos.add(new theObject(datos1));
contador ++;
}
br.close();
}
catch (IOException e) {
e.printStackTrace();
}
表格中填充了这一行:
tabla = new JTable(new MyModel(datos));
现在代码中涉及的类:
class theObject {
String sap,cte,pep,dis1,dis2,norma,nvent,ntc,tmando,tparalel,equip;
theObject(String dato[]) {
this.sap = dato[0];
this.cte = dato[1];
this.pep = dato[2];
this.dis1 = dato[3];
this.dis2 = dato[4];
this.norma = dato[5];
this.nvent = dato[6];
this.ntc = dato[7];
this.tmando = dato[8];
this.tparalel = dato[9];
this.equip = dato[10];
}
}
class MyModel extends AbstractTableModel {
private String[] columnNames = { "FERT","CLIENTE ","PEP","A","B","C","D","E","F","G","H" };
ArrayList<theObject> arr1 = null;
MyModel(ArrayList<theObject> arr1) {
this.arr1 = arr1;
}
public int getColumnCount() {
return columnNames.length;
}
public int getRowCount() {
return pru2.datos.size();
}
public Object getValueAt(int row, int col) {
theObject a1 = datos.get(row);
return a1;
}
}
在 Eclipse 中编译后没有错误,所以我无法确定问题
【问题讨论】:
-
我们无法调试不可见的代码。请发布您用来产生这些结果的代码。我立即看到的一件事是你可能不想要像
guiObject@4ab60e21这样的东西。这只是覆盖这些对象的toString方法的问题。 -
@Aaron 发布您的代码
-
对不起,我刚刚添加了一些代码
标签: java swing arraylist jtable tablemodel