1

I have the following problem: I try to populate a tableview in JavaFX8 with an array. So, I try to add the array as a row to the tableview. I run this code in the Controller of my FXML file, when enter is pressed. This is the code:

String[] words = {"ace", "boom", "crew", "dog", "eon"}; 
List<String> tableViewRow = Arrays.asList(words); 
ObservableList<String> row = FXCollections.observableList(tableViewRow); //observableArrayList also doesn't work
transactionOverview.getItems().add(row);

transactionOverview is my tableview, and the String[] is just a placeholder for my actual String[]. I tried to create my tableview in multiple ways:

public TableView<ObservableList<String>> transactionOverview;
public TableView<ObservableList> transactionOverview;
public TableView transactionOverview;

None of them works. The problem is that tableview gets an extra row, which I can select, but there are now string values visible in the tableview. I don't know if they are added.

My code is based on Javafx 2.2 - Dynamic table view - table data (answer from Jitendra Pareek), and I have chosen for this solution because I don't want to use an extra class to populate my tableview.

Any help is appreciated!

Community
  • 1
  • 1
bashoogzaad
  • 4,611
  • 8
  • 40
  • 65
  • Is there always an extra row? I mean what happens when the "words" has only one, two or no item. – Uluk Biy Oct 07 '14 at 10:26
  • The TableView always has 5 columns (fixed this in the FXML), and extra rows need to be added one by one. The amount of rows need to be different for every transaction. Does this answer your question, because I am not sure want you need to know? – bashoogzaad Oct 07 '14 at 10:29
  • Are you using a custom cell factory? – James_D Oct 07 '14 at 12:22
  • @James_D No, the only code I use to fill the tableview is presented above. – bashoogzaad Oct 07 '14 at 12:23
  • You presumably have a cell value factory somewhere (though it's probably not the issue). You might need to create a [MCVE](http://stackoverflow.com/help/mcve) for anyone to be able to help; the code you have posted looks fine to me. (Also, you'll probably find it much easier if you create a model class for the items in the table. This is the way the API was designed to be used.) – James_D Oct 07 '14 at 12:30
  • Thanks @James_D for the advice to use a model class. This is much simpler than I expected it to be. If anyone knows why the code above doesn't work, please feel free to answer. – bashoogzaad Oct 07 '14 at 14:25
  • Does your original code have a `cellValueFactory` attached to the columns at all? You should show it if it does. If not, then that's the issue. – James_D Oct 07 '14 at 14:27
  • No it didn't. but now I have a PropertyValueFactory tag inside the cellValueFactory tags. What should be in it considering the code above, where no model class is used? – bashoogzaad Oct 07 '14 at 14:29

1 Answers1

1

Since (according to your comments) you have a fixed number of columns, I would strongly recommend creating a model class to hold the items in each row of the table. You can then follow the standard patterns and it should work readily.

However you manage a TableView, you must provide a cell value factory for each column. This is essentially a function that specifies how to get the value for a cell from the item in the row. If your use a model class that uses JavaFX properties, then you can use a PropertyValueFactory (though Java 8 lambda expressions make that pretty much redundant). Otherwise, you need to implement a callback.

If you really want to use a list structure to hold the data for each row, and assuming your table and table columns are all defined in the FXML file, you would do something like this in your controller class:

@FXML
private TableView<ObservableList<String>> transactionOverview ;

// ...

public void initialize() {
    for (int i=0; i < transactionOverview.getColumns().size(); i++) {
        TableColumn<ObservableList<String>, String> col = transactionOverview.getColumns().get(i);
        final int colIndex = i ;
        col.setCellValueFactory( (CellDataFeatures cellData) -> {
            ObservableList<String> rowData = cellData.getValue();
            return new ReadOnlyStringWrapper(rowData.get(colIndex));
        });
    }

    // ...
}
James_D
  • 201,275
  • 16
  • 291
  • 322
  • Thank you very much for your help @James_D , I am now using the extra class, but I certainly need this in the future! – bashoogzaad Oct 08 '14 at 08:37