Stefan Sprenger
Stefan Sprenger

Reputation: 1080

JavaFX Span Tableview Merge Cells by MapEntries

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

Upvotes: 2

Views: 3905

Answers (1)

Stefan Sprenger
Stefan Sprenger

Reputation: 1080

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.

Upvotes: 4

Related Questions