【问题标题】:JTable populated by ArrayList, bad result由 ArrayList 填充的 JTable,结果不好
【发布时间】: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


【解决方案1】:

您为每个单元格获取gui$theObject@4ab60e21 的原因是因为您在getValueAt() 中返回整个theObject,它应该只返回theObject字段 例如,您的课程如下所示

public class TheObject {   // please notice the Naming convention
                           // and follow it. Class begins with capital
    private fert;
    private cliente;
    private pep;
    // getter and setters
}

getValueAt() 中的值应该只返回基于 col 的字段之一。例如

private String[] columnNames = { "FERT","CLIENTE ","PEP" };
private ArrayList<TheObject> data;
...
public Object getValueAt(int row, int col) {
    Object value = null;
    TheObject obj = data.get(row);
    switch(col) {
        case 0: value = obj.getFert(); break;
        case 1: value = obj.getCliente(); break;
        case 2: value = obj.getPep(); break;
        default: break;
    }
    return value;
}

因此,您的单元格值不会只是 TheObject 的一个字段,而不是整个 TheObject 对象。

【讨论】:

  • 非常感谢您的 cmets 是正确的,现在 mi table 工作正常。
猜你喜欢
  • 2012-01-27
  • 2018-12-14
  • 2017-12-29
  • 1970-01-01
  • 2016-04-03
  • 1970-01-01
  • 2015-09-08
  • 2012-05-26
  • 2022-01-10
相关资源
最近更新 更多