JavaFX Span Tableview Merge Cells by MapEntries

主宰稳场 提交于 2019-12-12 09:55:46

问题


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:


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.



来源:https://stackoverflow.com/questions/25400367/javafx-span-tableview-merge-cells-by-mapentries

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!