2
我有一个TreeTableView<MyCustomRow>
,我想动态添加列。在MyCustomRow
我有一个Map<Integer, SimpleBooleanProperty>
与行中的值。我以这种方式添加新列:JavaFx动态列值
private TreeTableColumn<MyCustomRow, Boolean> newColumn() {
TreeTableColumn<MyCustomRow, Boolean> column = new TreeTableColumn<>();
column.setId(String.valueOf(colNr));
column.setPrefWidth(150);
column.setCellValueFactory(data -> data.getValue().getValue().getValue(colNr));
column.setCellFactory(factory -> new CheckBoxTreeTableCell());
column.setEditable(true);
colNr++;
return column;
}
然后table.getColumns().add(newColumn())
。
问题是,当我连续检查一个CheckBox
时,该行中的所有复选框都被检查。这里是我行的代码:
public class MyCustomRow {
private Map<Integer, SimpleBooleanProperty> values = new HashMap<>();
public MyCustomRow(Map<Integer, Boolean> values) {
values.entrySet().forEach(entry -> this.values
.put(entry.getKey(), new SimpleBooleanProperty(entry.getValue())));
}
public SimpleBooleanProperty getValue(Integer colNr) {
if (!values.containsKey(colNr)) {
values.put(colNr, new SimpleBooleanProperty(false));
}
return values.get(colNr);
}
}
所以我设置取决于colNr
单元格的值,我也试着调试和似乎值在values
地图不同,所以我不知道为什么所有的复选框都选中时只检查一个。
非常感谢你,它工作得很好:) – Sunflame