JavaFX:将树表视图展开箭头与行内容对齐

问题描述 投票:0回答:2

下面是我的

TreeTableView
。正如您所看到的,用于展开或折叠级别的箭头与数据不一致。

enter image description here 我想让它们排成一行。但不知道该怎么做。我在css中尝试了以下方法,但都不起作用。

.tree-table-row-cell .arrow {
    -fx-alignment: CENTER;
    -fx-mark-color:red;
}
.tree-table-row-cell .arrow-button {
    -fx-alignment: CENTER;
    -fx-mark-color:blue;
}

.tree-table-cell .arrow {
    -fx-alignment: CENTER;
    -fx-mark-color:yellow;
}
.tree-table-cell .arrow-button {
    -fx-alignment: CENTER;
    -fx-mark-color:green;
} 
.tree-table-cell > .arrow-button >.arrow{
   -fx-alignment: CENTER;
   -fx-mark-color:purple;
}
.tree-table-row-cell > .arrow-button >.arrow{
   -fx-alignment: CENTER;
   -fx-mark-color:grey;
}

有人知道我怎样才能实现这一目标吗?

css javafx treetableview
2个回答
2
投票

这并不完美(自从提出这个问题以来已经过去了一年半),但我需要做同样的事情,这是我能想到的最好的。

问题是箭头已经居中对齐,并且在添加任何样式后它不会重新对齐,因此如果您添加任何使任何单元格内容变大的样式,它就会丢失它。 (我的一个单元格值使用较大的字体大小,因为该单元格中的值通常是一个减号,这对我的一些用户来说很难看到。)

使用Scenic View(解决此类问题的一个很好的工具)我发现箭头节点周围有一个类为.tree-disclosure-node的包装器。 (风景视图也是我知道它已经居中的方式。)所以我只是向该包装器添加了一些顶部填充。

不要费心尝试填充箭头本身,因为这只会使箭头变大。

希望这对某人有帮助。

.tree-disclosure-node {
    -fx-padding: 15 5 0 5;
}

enter image description here


0
投票

我发现了一个非常简洁的解决方案,通过提供一个自定义单元工厂,将树公开节点的高度绑定到单元高度:

public class ArrowCenteringFactory<T> implements Callback<TreeView<T>, TreeCell<T>>
{
  private Callback<TreeView<T>, TreeCell<T>> original = CheckBoxTreeCell.<T> forTreeView();

  @Override
  public TreeCell<T> call(TreeView<T> tree)
  {
    TreeCell<T> cell = original.call(tree);
    bindDisclosureNodeHeightToCellHeight(cell);
    return cell;
  }

  /**
   * The disclosure node is monitored being able to bind its height to the cell height in the same way {@link TreeCellSkin#layoutChildren} called by {@link Control#layoutChildren}.
   */
  private void bindDisclosureNodeHeightToCellHeight(TreeCell<T> cell)
  {
    cell.disclosureNodeProperty().addListener((ChangeListener<Node>) (observable, oldValue, newValue) ->
    {
      if (oldValue instanceof Region)
      {
        Region old = (Region) oldValue;
        old.prefHeightProperty().unbind();
      }
      if (newValue instanceof Region)
      {
        Region node = (Region) newValue;
        node.prefHeightProperty()
            .bind(Bindings.createDoubleBinding(() -> cell.snapSizeY(cell.getHeight()) - cell.snappedTopInset() - cell.snappedBottomInset(),
                                               cell.heightProperty(), cell.insetsProperty()));
      }
    });
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.