3

I'm fairly new to javafx, so please bear with me if my question is unclear. I've a TableView which need to be populated using an ObservableList.

The code below populates my "data" with the arraylists generated out of the Map, which in turn is used to add rows to my table.

TableView<ArrayList> table = new TableView<>();

ObservableList<ArrayList> data = FXCollections.observableArrayList();

     for(int i=0;i<listSelectedVerticesIds.size();i++){
          ArrayList<String> tempString = new ArrayList<>();
                for(Map.Entry<String,String> temp2 : mapVertex.get(listSelectedVerticesIds.get(i)).entrySet()){
                    tempString.add(temp2.getValue());
                }
                data.add(tempString);           
            }
    table.setItems(data);

However, I do not see the table populated with the list in "data". I'm guessing this is because there is no data binding (using setCellValueFactory). However, as you can see I dont have a data model class. All of my data comes from the Map as strings which I would like to populate in my tableview.

Please suggest.

3
  • I am assuming from this code that one row of data is represented by the arraylist in the observablelist? Meaning your table view would be something like this: TableView<ObservableList<String>> is that correct? Commented Mar 16, 2017 at 20:23
  • Yes, one row of data is an ArrayList. Basically, in the code, tempString has the values for one row of data. I'm adding multiple tempString ArrayList to "data", for multiple rows. Commented Mar 16, 2017 at 21:52
  • Can you post your table columns and their cell value factories? Commented Mar 17, 2017 at 13:27

1 Answer 1

1

Here is a simple way to do it that works great. You don't need a data structure to populate a table. You only see that because that's what most examples show. It is an extremely common use case to populate a table from a database or a file. I don't understand why this is so hard to find examples for. Well, hope this helps.

private TableView<ObservableList<StringProperty>> table = new TableView<>();
private ArrayList<String> myList = new ArrayList<>();

private void updateTableRow() {
    for (int row = 0; row < numberOfRows; row++) {
        ObservableList<StringProperty> data = FXCollections.observableArrayList();
        for (int column = 0; column < numberOfColumns; column++) {
            data.add(column, new SimpleStringProperty(myList.get(row + (column * numberOfRows))));
        }
        table.getItems().add(data);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.