TableColumn在场景中的位置 [英] position of TableColumn in Scene

查看:75
本文介绍了TableColumn在场景中的位置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有多个TableColumns的TableView,并且我想在某个TableColumn下放置一个Node.如何获取TableColumn的确切位置(x,y坐标),以便绑定节点的平移属性?

I have a TableView with several TableColumns and I want to place a Node below a certain TableColumn. How do I get the exact position (x,y-coordinates) of the TableColumn so I can bind the translate properties of my node?

以下是如何在TableView的右上角放置按钮的摘要:

Here is a snippet of how I placed a button on the top right corner of my TableView:

button.translateXProperty().unbind();
button.translateXProperty().bind(tableView.widthProperty().divide(2.0).subtract(button.getWidth() / 2.0 + 2.0) + tableView.localToScene(0.0, 0.0).getX());

这很好,但显然仅适用于TableView. TableColumns没有这些转换属性或localToScene方法,因此我无法直接获取要将Node绑定到的位置.

This works fine, but obviously only for the TableView. The TableColumns don't have those translate properties or the localToScene methods, so I can't directly get the position to which I would like to bind my Node.

我当前的解决方案(实际上效果不佳)是执行以下操作: 我在场景(PointA)中读出了TableView的位置,然后遍历所有列的列表(tableView.getColumns())并检查每列是否可见,如果有,将它们的宽度添加到X- PointA的值.我这样做直到找到要在其下面放置节点"的实际列. 现在的问题是,我不能真正将Nodes的位置绑定到这一点,因为当我更改列的顺序或使其中之一不可见时,我的列会在屏幕上更改位置.我必须将侦听器添加到列顺序和可见性...

My current solution (which doesn't really work that well) is to do the following: I read out the position of my TableView in the Scene (PointA) and then go through the list of all columns (tableView.getColumns()) and check if each of them is visible, and if so, add their width to the X-value of PointA. I do this until I find the actual column that I want to place the Node below. Now the problem is, that I can't really just bind the Nodes position to this point, because when I change the order of the columns, or make one of them invisible, my column changes position on the screen. I would have to add a listener to the column order and visibility...

有没有更有效的方式来做我想做的事? :D

Is there any more efficient way to do what I want? :D

推荐答案

如果允许使用非公共api,则可以考虑通过其外观访问TableColumnHeader,只要它的类型为TableViewSkinBase:它可以访问的api TableRowHeader,它是所有TableColumnHeaders的容器,并具有api查找包含的任何列的标题.

If you are allowed to use non-public api, you might consider to access the TableColumnHeader via its skin, provided it's of type TableViewSkinBase: it has api to access the TableRowHeader which is the container of all TableColumnHeaders and has api to find the header for any column it contains.

代码段(宽度/位置绑定是从James的示例中复制的,只是复制到标题而不是标签上)

Code snippet (the width/location binding is copied from James' example, just to the header instead of the label)

private void buttonsPerHeader(TableView<Person> table, Pane root) {
    if (!(table.getSkin() instanceof TableViewSkinBase)) return;
    TableViewSkinBase skin = (TableViewSkinBase) table.getSkin();
    TableHeaderRow headerRow = skin.getTableHeaderRow();
    for (TableColumn col : table.getColumns()) {
        TableColumnHeader header = headerRow.getColumnHeaderFor(col);
        Button button = new Button(col.getText());

        button.prefWidthProperty().bind(Bindings.createDoubleBinding(() -> 
            header.getBoundsInLocal().getWidth(), header.boundsInLocalProperty()));
        button.minWidthProperty().bind(button.prefWidthProperty());
        button.maxWidthProperty().bind(button.prefWidthProperty());

        button.layoutXProperty().bind(Bindings.createDoubleBinding(() -> 
            header.getLocalToSceneTransform().transform(header.getBoundsInLocal()).getMinX(),
            header.boundsInLocalProperty(), header.localToSceneTransformProperty()));

        button.layoutYProperty().bind(Bindings.createDoubleBinding(() ->
            table.getBoundsInParent().getMaxY() ,table.boundsInParentProperty()));

        root.getChildren().add(button);
    }

}

这篇关于TableColumn在场景中的位置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆