【问题标题】:JavaFx dynamic column valuesJavaFx 动态列值
【发布时间】:2017-10-23 05:00:03
【问题描述】:

我有一个TreeTableView<MyCustomRow>,我想动态添加列。在MyCustomRow 我有一个Map<Integer, SimpleBooleanProperty> 与行中的值。我以这种方式添加新列:

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 映射中的值不同,所以我不知道为什么当我检查所有复选框时只检查一个。

【问题讨论】:

    标签: javafx java-8 javafx-8 dynamic-columns treetableview


    【解决方案1】:

    在这一行,

    column.setCellValueFactory(data -> data.getValue().getValue().getValue(colNr));
    

    显示单元格时调用处理程序。因此所有colNr 都是最新值,最后一个索引的布尔属性与所有单元格相关联。

    newColumn()被调用时的值调用handler,例如:

    final Integer colNrFixed = colNr;
    column.setCellValueFactory(data -> data.getValue().getValue().getValue(colNrFixed));
    // ...
    colNr++;
    

    【讨论】:

      猜你喜欢
      • 2012-10-31
      • 1970-01-01
      • 2015-05-05
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多