Adding multiple row to a new Table object

edited October 2017 in How To...

There are many examples in the literature of Processing showing how to add rows to a Table object. However, they all seem to be blank rows or single rows. How do you add multiple rows to a new Table that you want to create? thanks

Answers

  • Use a for loop, and add a row in the body of the loop. This adds multiple rows, one at a time.

  • My application can't use a loop because the user provides the data at irregular intervals during other activities, so I must add rows one at a time interspersed with other code. The TableRow row=table.addRow() statement generates an error message about duplicating the row local variable if I repeat it for each new row. Perhaps I should use a function call to create additional rows. This would isolate the row variables from each other. I think that I should have perhaps reformatted my question and put it in the "questions about code" category.

    Thanks for answering

  • edited October 2017 Answer ✓

    @JBradley -- re:

    The TableRow row=table.addRow() statement generates an error message about duplicating the row local variable if I repeat it for each new row.

    You should share your code, or an MCVE of the affected portion of your code.

    Adding rows on an event is working fine for me. In this example, press some keys, then press escape to quite and get a table printout in the console.

    Table table;
    int count;
    
    void setup() {
      table = new Table();  
      table.addColumn("count", Table.INT);
      table.addColumn("time", Table.INT);
      table.addColumn("key", Table.STRING);
    }
    
    void draw(){
    }
    
    void keyReleased(){
      println(key);
      TableRow row = table.addRow();
      row.setInt("count", count++);
      row.setInt("time", millis());
      row.setString("key", key + "");
    }
    
    void exit(){
      for(TableRow row : table.rows() ){
        println( row.getInt(0), row.getInt(1), row.getString(2));
      }
    }
    
  • /*Thanks for your response, Jeremy. I did a little more research on the Processing web site and found why my original in line attempt failed, and have applied a very simple fix in the code below. Amazingly the solution was in the documentation for getRow(), and not in the documentation for addRow(). In the code below, I have included two ways for adding rows.

    One is the corrected version of my original in line attempt. The other is for a version which uses a function call with the Table included in the function arguments. The program does run and displays six vertical colored lines.

    My original in line version failed because I used the TableRow prefix on the first addRow statements in my add cyan and purple lines code. That is, TableRow row=myTable.addRow() can only be used on the first row. All subsequent statements must be row=myTable.addRow(). */

    Table myTable;
    int x1,x2,y1,y2;
    color c;
    
    void setup(){
    
    myTable=new Table();
    
    //add header
    
    myTable.addColumn("id");
    myTable.addColumn("x1");
    myTable.addColumn("y1");
    myTable.addColumn("x2");
    myTable.addColumn("y2");
    myTable.addColumn("color");
    
    //add rows to myTable
    
    //      This uses individual addRows in same code block
    
    // add a yellow line
    TableRow row =myTable.addRow();
      row.setInt("x1",125);
      row.setInt("y1",50);
      row.setInt("x2",125);
      row.setInt("y2",200);
      row.setInt("color",color(255,255,0));
    
     //  add a cyan line 
      row=myTable.addRow();
      row.setInt("x1",175);
      row.setInt("y1",50);
      row.setInt("x2",175);
      row.setInt("y2",200);
      row.setInt("color",color(0,255,255));
    
    //  add a purple line  
     row=myTable.addRow();
      row.setInt("x1",225);
      row.setInt("y1",50);
      row.setInt("x2",225);
      row.setInt("y2",200);
      row.setInt("color",color(255,0,255)); 
    
    //          This way works with function calls
    
    //add a red line
    addLineRow(100,50,100,200, color(255,0,0), myTable);
    
     //add a green line
     addLineRow(150,50,150,200, color(0,255,0),myTable);
    
     //add blue line
     addLineRow(200,50,200,200, color(0,0,255),myTable);
    
     strokeWeight(4);
     background(150);
     size(300,300);
    
    }
    void addLineRow(int ex1, int ey1, int ex2, int ey2, color c, Table table){
    
      //Table TableRow ;
      TableRow row =table.addRow();
      row.setInt("x1",ex1);
      row.setInt("y1",ey1);
      row.setInt("x2",ex2);
      row.setInt("y2",ey2);
      row.setInt("color",color(c));
    
    
    }
     void draw(){
    
    //read and print all six rows
    
    for(TableRow row: myTable.rows()){
    
    x1=row.getInt("x1");
    y1=row.getInt("y1");
    x2=row.getInt("x2");
    y2=row.getInt("y2");
    c= row.getInt("color");
    stroke(c);
    line(x1,y1,x2,y2 );
    
    
    } 
    } 
    
  • edited October 2017 Answer ✓

    @JBradley -- re:

    That is, TableRow row=myTable.addRow() can only be used on the first row. All subsequent statements must be row=myTable.addRow().

    That is not anything specific to Table, or TableRows. That is a feature of the Java and Processing programming languages for any object or primitive of any type. Declare something once, use it many times.

    If you declare it over and over, you keep replacing the old thing with a brand new thing.

    int foo = 0;
    foo = foo + 1; // "1"
    foo = foo + 1; // "2"
    foo = foo + 1; // "3"
    
    // won't compile
    int foo = 0;
    int foo = foo + 1; // "1?"
    int foo = foo + 1; // "1?"
    int foo = foo + 1; // "1?"
    

    In most cases this will simply not be allowed and an error will be generated:

    Duplicate local variable foo

  • Thank you Jeremy. A good lesson for me. ;;)

Sign In or Register to comment.