如何在节点对象的状态发生变化时动态更改JTree视图?

时间:2021-03-07 12:32:46

I'm implementing a Java JTree panel. This panel holds a TreeModel build from a set ofdatastructures that are treelike (a list of lists of composites - different classes). I get these datastructure from external jar implementations based on a set of interfaces I defined.

我正在实现一个Java JTree面板。此面板包含一组树状结构的TreeModel构建,这些结构是树状的(复合材料列表列表 - 不同的类)。我根据我定义的一组接口从外部jar实现中获取这些数据结构。

The treenodes contain a checkbox that the user may check to indicate that the checked node and all child nodes are to become "active", that is, the objects that are represented by the nodes should do something, like getting data from a database.

treenode包含一个复选框,用户可以检查该复选框以指示已检查的节点和所有子节点将变为“活动”,即,由节点表示的对象应该执行某些操作,例如从数据库获取数据。

The treenodes may also be selected without "activating" them, that is, without the checkbox being checked.

也可以在不“激活”它们的情况下选择treenode,也就是说,不选中复选框。

On top of that, other parts of the program may also toggle the activation state of the datamodel objects. So the data model out of which the treemodel is build is the source of the activation state. This must be reflected in the treeview by dynamically (un)checking the checkbox.

最重要的是,程序的其他部分也可以切换数据模型对象的激活状态。因此,构建树模型的数据模型是激活状态的来源。这必须通过动态(取消)选中复选框反映在树视图中。

Now, how do I implement this whole? Who should be listeners for what changes?

现在,我该如何实现这一整体?谁应该成为听众的变化?

I now have all the classes that are in the nodes extend from an abstract class that holds an activation field. This is the true datasource. When this field changes, all subscribed listeners (EventListener) should be notified, This includes the checkboxes.

我现在让节点中的所有类都从包含激活字段的抽象类扩展。这是真正的数据源。当此字段更改时,应通知所有订阅的侦听器(EventListener),这包括复选框。

I also have a TreeSelectionModel that is based on the default TreeSelectionModel but extended with functionality to check if children/parents need to be checked.

我还有一个TreeSelectionModel,它基于默认的TreeSelectionModel,但扩展了功能,以检查是否需要检查子/父。

My questions maybe is not really clear, but so is this complex piece of code. Hope you can help.

我的问题可能并不是很清楚,但这段复杂的代码也是如此。希望你能帮忙。

3 个解决方案

#1


You seem to have a bunch of moving parts. It might be good to funnel all changes through your TreeModel, and have yourJTree be the listener, as it will be automatically. If your TreeModel is a subclass of DefaultTreeModel, you get a host of fire* (fireTableStructureChanged, fireTreeNodesChanged, etc.) methods which will alert your JTree to repaint. Make sure you do this firing of events in the AWT EventQueue.

你好像有一堆活动部件。通过TreeModel汇集所有更改可能会很好,并且让yourJTree成为侦听器,因为它将自动进行。如果您的TreeModel是DefaultTreeModel的子类,则会获得一系列fire *(fireTableStructureChanged,fireTreeNodesChanged等)方法,这些方法将提醒您的JTree重新绘制。确保在AWT EventQueue中执行此事件触发。

here is a good tutorial with a load of examples for using Trees.

这是一个很好的教程,里面有很多使用Trees的例子。

#2


Check out the jide components. It includes a checkboxtree also:

查看jide组件。它还包括checkboxtree:

如何在节点对象的状态发生变化时动态更改JTree视图?

#3


It sounds to me like you have the right idea -- make the node the canonical source for the "activated" knowledge, and the canonical source of activation events. Then you can have the TreeModel listen to that and translate those events into fireTreeNodesChanged() etc, which should cause the JTree to update itself automatically.

听起来我觉得你有正确的想法 - 让节点成为“激活”知识的规范来源,以及激活事件的规范来源。然后你可以让TreeModel监听它并将这些事件转换为fireTreeNodesChanged()等,这将导致JTree自动更新。

To keep yourself out of trouble, try to keep the node -> tree event relationship one way -- that is, don't use the tree as a controller, don't allow tree events to change the activation state.

为了避免麻烦,尝试保持node - > tree事件关系的一种方式 - 也就是说,不要将树用作控制器,不要让树事件更改激活状态。

#1


You seem to have a bunch of moving parts. It might be good to funnel all changes through your TreeModel, and have yourJTree be the listener, as it will be automatically. If your TreeModel is a subclass of DefaultTreeModel, you get a host of fire* (fireTableStructureChanged, fireTreeNodesChanged, etc.) methods which will alert your JTree to repaint. Make sure you do this firing of events in the AWT EventQueue.

你好像有一堆活动部件。通过TreeModel汇集所有更改可能会很好,并且让yourJTree成为侦听器,因为它将自动进行。如果您的TreeModel是DefaultTreeModel的子类,则会获得一系列fire *(fireTableStructureChanged,fireTreeNodesChanged等)方法,这些方法将提醒您的JTree重新绘制。确保在AWT EventQueue中执行此事件触发。

here is a good tutorial with a load of examples for using Trees.

这是一个很好的教程,里面有很多使用Trees的例子。

#2


Check out the jide components. It includes a checkboxtree also:

查看jide组件。它还包括checkboxtree:

如何在节点对象的状态发生变化时动态更改JTree视图?

#3


It sounds to me like you have the right idea -- make the node the canonical source for the "activated" knowledge, and the canonical source of activation events. Then you can have the TreeModel listen to that and translate those events into fireTreeNodesChanged() etc, which should cause the JTree to update itself automatically.

听起来我觉得你有正确的想法 - 让节点成为“激活”知识的规范来源,以及激活事件的规范来源。然后你可以让TreeModel监听它并将这些事件转换为fireTreeNodesChanged()等,这将导致JTree自动更新。

To keep yourself out of trouble, try to keep the node -> tree event relationship one way -- that is, don't use the tree as a controller, don't allow tree events to change the activation state.

为了避免麻烦,尝试保持node - > tree事件关系的一种方式 - 也就是说,不要将树用作控制器,不要让树事件更改激活状态。