简体   繁体   English

Java JTable矢量与对象填充

[英]Java JTable vector with objects filling

I've been trying to fill a JTable for about three days. 我一直在努力填补JTable大约三天。 All I need to do is fill a vector of vectors with "Artikel" objects, fill a header vector and bind these two vectors to a JTable. 我需要做的就是用“Artikel”对象填充向量矢量,填充标题向量并将这两个向量绑定到JTable。

I could manage this with using a custom AbstractTableModel but I couldn't create a addColumn() method. 我可以使用自定义AbstractTableModel来管理它,但我无法创建addColumn()方法。 So, I gave up this way. 所以,我放弃了这种方式。 Now I just use standard DefaultTableModel but now I can't get my JTable right filled. 现在我只使用标准的DefaultTableModel,但现在我无法正确填充JTable。 I get all my objects in the first column instead of separated to the all columns: fault screenshot 我在第一列中获取所有对象,而不是分隔到所有列: fault screenshot

GUI

My Artikel class: 我的Artikel课程:

public class Artikel {

private String EnitiativeRef;
private String Brand;
private String pnb;
.
.
.
public Artikel(){        
}

public String getEnitiativeRef() {
    return EnitiativeRef;
}

public void setEnitiativeRef(String EnitiativeRef) {
    this.EnitiativeRef = EnitiativeRef;
}
.
.
.
}

My button code: 我的按钮代码:

private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {

    ICsvBeanReader inFile = null;
    String[] header = {};
    Vector<Vector<Artikel>> data = null;

    try {
        inFile = new CsvBeanReader(new FileReader("C:\\609661920071022111.csv"), CsvPreference.STANDARD_PREFERENCE);

        header = inFile.getHeader(true);

        data = new Vector<Vector<Artikel>>();

        Artikel artikel;
        while ((artikel = inFile.read(Artikel.class, header, cellProcessor)) != null) {
            Vector<Artikel> tmpVector = new Vector<Artikel>();
            tmpVector.addElement(artikel);
            data.addElement(tmpVector);
        }

    } catch (Exception ex) {
        System.out.println("FOUT: " + ex.toString());
    } finally {
        try {
            inFile.close();
        } catch (IOException ex) {
            Logger.getLogger(main.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    tblAll.setModel(new DefaultTableModel(data, new Vector(Arrays.asList(header))));
    tblAll.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
}

Can you tell me what am I doing wrong or guide me to the right way of doing this? 你能告诉我我做错了什么或指导我做正确的方法吗? I will really appreciate your grateful help. 我将非常感谢您的感谢。

Each element in the vector of vectors represents a row and each element of the those element vectors represent a column. 向量矢量中的每个元素表示一行,这些元素矢量的每个元素表示一列。

You are adding one-element vectors to the main vector, and the element is an object of the class for which you haven't implemented the toString method. 您正在向主向量添加单元素向量,并且该元素是您尚未实现toString方法的类的对象。

You are probably going the wrong way. 你可能走错了路。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM