How to find a row in a table?

edited February 2017 in How To...

I have a CSV file. I have loaded it in with loadTable() and I am trying to find which row number has a letter in it with

TableRow result = navTable.findRow(facing, "Room");

However I can't figure out how to get the row number from 'result'. The documentation is extremely vague and confusing,

Can anyone help please?

Thanks,

Mike

Answers

  • edited February 2015
    • By definition, a TableRow wouldn't have any recollection from which row index number it once belonged.
    • Surprisingly, it indeed stores its row index internally. But doesn't allow us any access to it.
    • However we can forcibly access it by using an advanced Java technique called "Reflection".
    • In this particular case we wanna capture TableRow's row Field.
    • It's somewhat pretty much complicated. In short we're gonna need:
      getClass() -> getDeclaredField() -> setAccessible() -> getInt().
    • Here's my solution. Just invoke getRowIndex() passing your TableRow object:

    "FieldHack.pde":

    /**
     * TableRow's Row Field Hack (v1.0)
     * by GoToLoop (2015/Feb/13)
     *
     * forum.Processing.org/two/discussion/9412/how-to-find-a-row-in-a-table
     * docs.Oracle.com/javase/8/docs/api/java/lang/reflect/Field.html
     */
    
    import java.lang.reflect.Field;
    
    static final int getRowIndex(TableRow tr) {
      Class<? extends TableRow> c = tr.getClass();
      Field f = getField(c, "row");
      f.setAccessible(true);
      return readIntField(f, tr);
    }
    
    static final Field getField(Class c, String s) {
      try {
        return c.getDeclaredField(s);
      }
    
      catch (NoSuchFieldException cause) {
        throw new RuntimeException(cause);
      }
    }
    
    static final int readIntField(Field f, Object p) {
      try {
        return f.getInt(p);
      }
    
      catch (IllegalAccessException cause) {
        throw new RuntimeException(cause);
      }
    }
    
  • thank you. it's very useful.

  • The correct answer is to use findRowIndex() instead of findRow(), which already does exactly what the poster wanted.

  • @fry It would be very helpful if the information could be added to the public reference. The information already exists in the JavaDoc, but most don't know how to find information there.

  • We just have to make tradeoffs about how much detail to include in the basic reference, versus all the complete parts in the JavaDoc. If we include everything, people getting started with the Table class get overwhelmed and say "that's not for me." If we include too little, more advanced users get disappointed.

    But we change things from time-to-time, for instance: https://github.com/processing/processing-docs/issues/402#issuecomment-260007810

    The JavaDoc link is prominently noted in the reference, however: https://processing.org/reference/Table.html And I think at the moment, the page is already pretty overwhelming. See my comment at the Github link—the best solution would also be a tutorial and more information about Table, but I haven't had time to write it, and nobody else has contributed one.

  • Ok, I guess you're right @fry.

  • The correct answer is to use findRowIndex() instead of findRow(), ...

    Well, my solution was from Feb 2015. I've got my doubts findRowIndex() existed back then... :-?

  • That was before a stable release of P3 even existed, if I'm right.

Sign In or Register to comment.