JavaFX Span Tableview Merge Cells by MapEntries


Question

Hello i have the following Map:

Map<String,ArrayList>

I would like to have a TableView like this

|--------------|-----------|
|ArrayList e1  | String e  |
|--------------|           |
|ArrayList e2  |           |
|--------------|           |
|ArrayList e3  |           |
|--------------|-----------|
|ArrayList x1  | String x  |
|--------------|           |
|ArrayList x2  |           |
|--------------|-----------|

I already tried it with several CellValueFactory Callbacks, but i don't have any clue how to read out my values and, span or merge these cells.

Best regards

1
2
8/22/2014 7:44:22 AM

Accepted Answer

I solved it by creating a CellValueFactory for the first column where I grabbed the ArrayList as String so:

    arrayListCol.setCellValueFactory(new Callback<TableColumn.CellDataFeatures<Map.Entry<PropertyDifference, DifferenceFileList>, String>, ObservableValue<String>>() {
        @Override
        public ObservableValue<String> call(TableColumn.CellDataFeatures<Map.Entry<PropertyDifference, DifferenceFileList>, String> p) {
            return new SimpleStringProperty(Arrays.toString(p.getValue().getValue().getFileList().toArray()));
        }
    });

results in:

|--------------|-----------|
|[e1,e2,e3]    | String e  |
|--------------|-----------|
|[x1,x2]       | String x  |
|--------------|-----------|

This handles the values for the column entries. Now i thought about a further representation form and used a CellFactory for this, which afterward formats the cell.

    Callback<TableColumn<Map.Entry<PropertyDifference, DifferenceFileList>, String>, TableCell<Map.Entry<PropertyDifference, DifferenceFileList>, String>> tableCellList = new Callback<TableColumn<Map.Entry<PropertyDifference, DifferenceFileList>, String>, TableCell<Map.Entry<PropertyDifference, DifferenceFileList>, String>>() {
        @Override
        public TableCell<Map.Entry<PropertyDifference, DifferenceFileList>, String> call(TableColumn<Map.Entry<PropertyDifference, DifferenceFileList>, String> param) {
            return new TableCell<Map.Entry<PropertyDifference, DifferenceFileList>, String>() {
                @Override
                protected void updateItem(String item, boolean empty) {
                  if (item != null) {
                      item = item.replace("[", "") .replace("]", "");
                      ObservableList<String> items = FXCollections.observableArrayList(item.split(","));
                      final ListView<String> listView = new ListView<String>();

                      listView.setItems(items);

                      setGraphic(listView);
                  }
                }
            };
        }
    };

    arrayListCol.setCellFactory(tableCellList);

This replaces the "[" and "]" chars and splits the "stringed" ArrayList into an ObservableList by using "," as a split delimeter.

The ObservableList is used for a ListView which is then added to the cell via the line:

setGraphic(listView);

.

|------|-----------|
|  e1  | String e  |
|  e2  |           |
|  e3  |           |
|------|-----------|
|  x1  | String x  |
|  x2  |           |
|------|-----------|

Anything unclear? -> just comment.

4
8/22/2014 7:58:58 AM

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