简体   繁体   中英

Java JTable vector with objects filling

I've been trying to fill a JTable for about three days. 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.

I could manage this with using a custom AbstractTableModel but I couldn't create a addColumn() method. So, I gave up this way. Now I just use standard DefaultTableModel but now I can't get my JTable right filled. I get all my objects in the first column instead of separated to the all columns: fault screenshot

GUI

My Artikel class:

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.

You are probably going the wrong way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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