JavaFx:当Treeitem不在View中时,在TreeView中仅需要滚动到索引号 [英] JavaFx: In TreeView Need Only Scroll to Index number when treeitem is out of View
问题描述
我需要做一个简单的事情.我有一个大的TreeView.以及菜单项下一个和上一个.在下一步,我必须选择下一个树项.树形列表看起来像这样
I need to do a simple thing. I have a large 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.
让我手动选择第一项.在此之后,我按下一步按钮.现在,这会导致一个奇怪的行为,即它总是在新选定的树状对象在视图中(可查看区域的边界)或视图外滚动时滚动.假设我有很多树木项目.而我的要求是,我只希望仅在新选择的树项不可见时才发生滚动.任何人都可以建议如何实现这一目标吗?
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??
谢谢.
推荐答案
这个简单的事情并不是那么简单.这是JavaFX中的一个已知问题.参见问题RT-18965 .
This simple thing is not so simple. It is a known issue in JavaFX; see issue RT-18965.
基于问题中的评论,我使其与我自己的TreeView一起使用.请注意,正如问题注释中所述,我依靠JavaFX中的内部类来获得最终结果.无法保证这些内部类将继续存在.
Based on the comment in the issue, I have made it work with my own TreeView. Please note that - as in the issue comment - I have relied on internal classes within JavaFX to get my end result. 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;
}
}
第二,将该皮肤应用于您关注的TreeView实例.
Second, apply that skin to the TreeView instance you are concerned about.
myTreeView.setSkin(new FolderTreeViewSkin(myTreeView));
最后,将新方法用作scrollTo
之前的条件:
Finally, use the new method as a condition before 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.
这篇关于JavaFx:当Treeitem不在View中时,在TreeView中仅需要滚动到索引号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!