使用JavaFX中的treeItem获取treeCell实现 [英] Get the treeCell Implementation using the treeItem in JavaFX
问题描述
我正在两个树视图之间实现拖动和拖动。当treeItem被拖放到treeItem的另一个treeView时,在两个treeItem之间建立了一个线连接。这工作正常,但最初没有拖放事件的连接对我来说是个问题。
我正在使用treeCell进行拖放事件。
正如我想的那样,事实证明这很难。
根据设计,无法获得属于给定 TreeItem
的 TreeCell
。这是因为 TreeView
的虚拟化设计:创建了最小数量的 TreeCell
,这些是根据需要使用新的 TreeItem
进行更新。因此,对于每个 TreeItem
,以及 TreeItem c>,通常不会有
TreeCell
由给定 TreeCell
表示的code>可能随时发生变化。
为了使这项工作,首先创建一个可观察的集合,存储树之间的所有连接(例如 ObservableSet
应该可以正常工作)。通过某个类来表示连接,该类公开可用于行的起点和终点。
为树创建自定义单元工厂。他们返回的单元格应该是:
- 观察他们所代表的项目。如果项目更改为位于一个或多个连接的末尾的项目,则应将这些连接上的相应点绑定到单元格坐标的相应变换。
- If项目从一个或多个连接的末尾更改 ,然后从单元格坐标取消绑定相应的结尾
- 观察可观察的连接集合。如果添加了一个此单元格的项目是一端,则绑定上面的坐标
请注意,绑定坐标时,您需要考虑细胞可能移动的事实(例如,通过滚动或通过GUI布局中的其他更改)。您还需要将坐标从单元格自己的坐标系转换为坐标系统,无论哪个窗格连接到另一个窗格,显然,它必须是两棵树的一些常见场景图形祖先。 / p>
最后,你需要一些家务。连接需要确保它们变得不可见,或者如果它们不再绑定在一端或多端,则从场景中移除。
我创建了一个例子。我刚刚创建了一些用于生成连接的简单控件,但您可以通过拖放轻松完成此操作。封装连接视图的类是 AssignmentView
;它使用 Assignment
来表示连接的实际数据。 ConnectedTrees
类是主应用程序,大多数有趣的控制器类型工作都在那里。其余的类只是数据表示。这个例子都是Java 8;我认为它在JavaFX 2.2中会更加丑陋。
I am implementing drag and drag between two tree views. When a treeItem is dropped onto another treeView of treeItem a line connection is established between the two treeItems. This is working fine , but to have a connection initially without a drag and drop events is problem to me. I am using treeCell for the drag and drop events.
As I thought, this turns out to be pretty difficult.
There is, by design, no way to get the TreeCell
belonging to a given TreeItem
. This is because of the "virtualization" design of the TreeView
: a minimal number of TreeCell
s are created and these are updated with new TreeItem
s as required. Thus there typically won't be a TreeCell
for every TreeItem
, and the TreeItem
represented by a given TreeCell
may change at any point.
To make this work, first create an observable collection storing all the connections between the trees (e.g. an ObservableSet
should work well). Represent the connections by some class that exposes start and end points which can be used for the lines.
Create custom cell factories for the trees. The cells they return should:
- observe the item they are representing. If the item changes to one that is at an end of one or more connections, then the appropriate point on those connections should be bound to the appropriate transform of the coordinates of the cell.
- If the item changes from one that is at the end of one or more connections, then unbind the appropriate end from the cell coordinates
- observe the observable collection of connections. If one is added for which this cell's item is one end, then bind the coordinates as above
Note that when you bind the coordinates, you need to take into account the fact that the cells may move (e.g. via scrolling or via other changes in GUI layout). You also need to transform the coordinates from the cell's own coordinate system into the coordinate system of whichever pane is holding the connections (obviously, if these are connecting one tree to another, it must be some common scene graph ancestor of both trees).
And finally, you need some housekeeping. The connections need to make sure they either become invisible, or are removed from the scene if they are no longer bound at one or more ends.
I created an example. I just created some simple controls for generating the connections, but you could easily do this with drag and drop instead. The class encapsulating the view of the connection is AssignmentView
; it uses Assignment
to represent the actual data that is connected. The ConnectedTrees
class is the main application and most of the interesting controller-type work is in there. The remaining classes are just data representation. The example is all Java 8; I think it would be much uglier in JavaFX 2.2.
这篇关于使用JavaFX中的treeItem获取treeCell实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!