3

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.

9
  • For better help sooner, post an SSCCE. Hard-code values for the CSV data. Commented Nov 11, 2012 at 23:38
  • 2
    Note: potentially long running tasks (file I/O, DB access..) should not be done on the EDT. GUI updates should only be done on the EDT. That code seems to be mixing the two. See Concurrency in Swing for more details. Commented Nov 11, 2012 at 23:40
  • 2
    @AndrewThompson: I'm not sure I agree with your last comment. It's a sign of no adequate toString() override, but not of arrays. Commented Nov 11, 2012 at 23:45
  • 1
    Looks to me that you are not filling the DefaultTableModel correctly. You are adding Artikels while you should add each value of each column independently. Anyway, it would be a lot easier if you extend AbstractTableModel and rely on you Vector<Artikel> Commented Nov 11, 2012 at 23:49
  • 1
    @AndrewThompson: your suggestion about 'Concurrency in Swing' was something I thought but never knew what/how it is, thanks;) Commented Nov 13, 2012 at 9:52

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you all for your responses guys! Now I begin to see what I'm doing wrong.I've just implemented the toString method in the 'Artikel' class as follows: @Override public String toString(){return EnitiativeRef + " " + Brand + " " + pnb;} The result still appears in the first column but this time each object's values appended toegether as one row:screenshot. I know this behaviour is quite normal because of my toString implemantation. Can you give me an example with adding each value of each column independently using DefaultTableModel?
The problem is solved by using objects inside vector instead of vectors in vector. So, changed the Vector<Vector<Artikel>> data to Vector<Artikel> data . I can also do now anything I want with a standard custom AbstractTableModel. The trick is updating vector and then model if you want to make a change in the JTabel(update record,add column..). Thank you all for your responses and made me think the right way of updating JTable.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.