Reputation: 2814
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
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.
Upvotes: 3
Views: 2627
Reputation: 51030
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.
Upvotes: 1