zdi301
zdi301

Reputation: 55

How to put data in JTable from Vector with java

I have a table which has only field names and no data, I want to enter data from inputs by Vector

i try this cod but is't work

 Object[][] ss= new frand[1][];
      for(int i = 0 ; i<feeds.size();i++){
          ss[1][i]=feeds.get(i);

      }
    JTable table = new JTable(
//      new Object[][] {
//        new frand[] { feeds.get(0) },
//        new frand[] { feeds.get(1) }
//      },
            ss,
      new String[] { "connect client " }
    );
  frandtabmodule module =  new frandtabmodule(feeds);
    table.setModel(module);

how do I do that?

Upvotes: 0

Views: 5164

Answers (2)

tenorsax
tenorsax

Reputation: 21233

It seems that you first create a table by passing data and columns, then create a model and set the model. When you pass column and row data in JTable constructor, then the table creates and uses DefaultTableModel that is initialized with that data. If you have a specialized model frandtabmodule that wraps around the original vector feeds there is no need to build an array and pass it to the table. Just use JTable default constructor and then call setModel, or use a constructor that takes a model as an argument.

See How to use Tables for more details and examples.

EDIT:

Not sure is that is the intention, but the posted code indicates that you want to use the vector elements as columns. If that is not the intention, then it seems you have a mix up of indexes while building an array. The first set of square brackets is for the rows and the second is for columns. For example:

private static Object[][] vector2DArray(Vector<Object> sourceVector) {
    Object[][] rows = new Object[sourceVector.size()][1];

    for (int i = 0; i < sourceVector.size(); i++) {
        rows[i][0] = sourceVector.get(i);
    }

    return rows;
}

Upvotes: 1

santiago92
santiago92

Reputation: 297

to load the table from an array A, if the table just create it, you can simply use:   

     table.setModel (new javax.swing.table.DefaultTableModel (
                 new String [] [] {
 {"2014-02-22", A.get(0)}
     }   ,          
                 new String [] {
             "Date", "Total" 
         }));

if not, you can use the DefaultTableModel class and its methods and AddColumn addRow

Upvotes: 1

Related Questions