简体   繁体   English

JavaFx:当Treeitem不在视图中时,在TreeView中仅需要滚动到索引号

[英]JavaFx: In TreeView Need Only Scroll to Index number when treeitem is out of View

I need to do a simple thing. 我需要做一件简单的事情。 I have a large TreeView. 我有一个大的TreeView。 and a menu item Next and Previous. 以及菜单项下一个和上一个。 on Next I have to select next Tree Item. 在下一步,我必须选择下一个树项。 Tree List Look like this 树形列表看起来像这样

-Root(set visible hide is true for root)
 --Parent 1
 ----Child 1
 ----Child 2
 ----Child 3
 --Parent 2
 ----Child 1
 ----Child 2

Now By pressing Next or previous menu item i call 现在,通过按“下一个”或“上一个”菜单项,我致电

myTreeView.getSelectionModel().clearAndSelect(newIndex);

By This I have manage to select next item and by 通过这个,我设法选择下一个项目,并通过

myTreeView.getSelectionModel().scrollTo(newIndex) 

i have manage to scroll to selected treeitem. 我已经设法滚动到选定的treeitem。

Problam: Problam:

Let I select the first item manually. 让我手动选择第一项。 after this i press next button. 之后,我按下一步按钮。 Now This cause a weird behavior that it always scrolls weather new selected treeitem is in view (bounds of view able area) or out of view. 现在,这会导致一个奇怪的行为,即它总是在新的选定树项目在视图中(可查看区域的边界)或视图之外滚动。 let assume i have large list of tree items. 假设我有很多树木项目。 and my requirement is just i only want scroll happened only when new selected tree item go out of view. 我的要求是我只希望仅在新选择的树项不可见时才发生滚动。 can any body suggest how to achieve this?? 任何人都可以建议如何实现这一目标吗?

Thanks. 谢谢。

This simple thing is not so simple. 这个简单的事情并不是那么简单。 It is a known issue in JavaFX; 这是JavaFX中的一个已知问题。 see issue RT-18965 . 参见问题RT-18965

Based on the comment in the issue, I have made it work with my own TreeView. 根据问题中的评论,我使其与我自己的TreeView一起使用。 Please note that - as in the issue comment - I have relied on internal classes within JavaFX to get my end result. 请注意,正如问题注释中所述,我依靠JavaFX中的内部类来获得最终结果。 There is no guarantee that these internal classes will continue to be. 无法保证这些内部类将继续存在。

First, create a new Skin that is derived from an internal class (again: dangerous): 首先,创建一个从内部类派生的新外观(再次:危险):

import javafx.scene.control.TreeView;
import com.sun.javafx.scene.control.skin.TreeViewSkin;
import com.mycompany.mymodel.DataNode;

/**
 * Only done as a workaround until https://javafx-jira.kenai.com/browse/RT-18965
 * is resolved.
 */
public class FolderTreeViewSkin extends TreeViewSkin<DataNode>
{
    public FolderTreeViewSkin(TreeView<DataNode> treeView)
    {
        super(treeView);
    }

    public boolean isIndexVisible(int index)
    {
        if (flow.getFirstVisibleCell() != null &&
            flow.getLastVisibleCell() != null &&
            flow.getFirstVisibleCell().getIndex() <= index &&
            flow.getLastVisibleCell().getIndex() >= index)
            return true;
        return false;
    }
}

Second, apply that skin to the TreeView instance you are concerned about. 其次,将该皮肤应用于您关注的TreeView实例。

myTreeView.setSkin(new FolderTreeViewSkin(myTreeView));

Finally, use the new method as a condition before scrollTo : 最后,使用new方法作为scrollTo之前的条件:

if (!((FolderTreeViewSkin) myTreeView.getSkin()).isIndexVisible( intThePositionToShow ))
    myTreeView.scrollTo( intThePositionToShow );

This is what worked for me; 这就是对我有用的东西; I hope it can help you or anyone else. 我希望它可以帮助您或其他任何人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM