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.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Korki Korkig
  • 2,736
  • 9
  • 34
  • 51
  • For better help sooner, post an [SSCCE](http://sscce.org/). Hard-code values for the CSV data. – Andrew Thompson Nov 11 '12 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](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. – Andrew Thompson Nov 11 '12 at 23:40
  • `bs.Artikel@67780b07` is the form usually used for printing arrays or collections. – Andrew Thompson Nov 11 '12 at 23:43
  • 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. – Hovercraft Full Of Eels Nov 11 '12 at 23:45
  • @HovercraftFullOfEels You are probably correct. :P – Andrew Thompson Nov 11 '12 at 23:46
  • 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` – Guillaume Polet Nov 11 '12 at 23:49
  • See also this [related example](http://stackoverflow.com/questions/13274644/how-i-can-use-arraylist-to-store-my-data-in-predefined-default-table-model-for-j/13275274#13275274) – Guillaume Polet Nov 11 '12 at 23:58
  • If you're interested in hiding/showing columns, you might like to have a look at [this](http://stackoverflow.com/questions/1492217/how-to-make-a-columns-in-jtable-invisible-for-swing-java) and [this](http://www.stephenkelvin.de/XTableColumnModel/). I personal prefer the second approach, but that's just me ;) – MadProgrammer Nov 12 '12 at 00:09
  • 1
    @AndrewThompson: your suggestion about 'Concurrency in Swing' was something I thought but never knew what/how it is, thanks;) – Korki Korkig Nov 13 '12 at 09:52

1 Answers1

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.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • 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](http://www.okyanus.be/vraag2.jpg). 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? – Korki Korkig Nov 13 '12 at 10:22
  • The problem is solved by using objects inside vector instead of vectors in vector. So, changed the `Vector> data` to `Vector 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. – Korki Korkig Nov 28 '12 at 13:59