double_o
double_o

Reputation: 69

Loop through column of tableview and get cell data

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?

Upvotes: 1

Views: 5755

Answers (2)

scs
scs

Reputation: 575

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));
}

Upvotes: 0

Cobbles
Cobbles

Reputation: 1798

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.

Upvotes: 3

Related Questions