Loop through column of tableview and get cell data


Question

I have a TableView with two Columns (let's call them A and B). I like to loop through column A and print their values to console. My code doesn't seem to work the way I want it to....

for (int i : myTable.getItems().size()) {
      System.out.print(columnA.getCellData(i));
}

Suggestions?

1
0
6/7/2014 10:22:30 AM

Accepted Answer

You almost had it! But I don't think that for loop is valid - it expects an Array not an int.

 for (Object o : myTable.getItems()) {
        System.err.println(columnA.getCellData(o));
 }

Or if you are using Java 8, this is a shorter way:

myTable.getItems().stream().forEach((o)
            -> System.err.println(columnA.getCellData(o)));

This works for me. If it doesn't just comment and I'll see what's wrong.

2
6/7/2014 10:43:52 AM

I just checked the code and found a small correction - the type of the item for getCellData() must be String not object. For the first example, this would result in:

for (String[] o : myTable.getItems()) {
  System.err.println(columnA.getCellData(o));
}

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon