JavaFX:如何在无法再访问属性时收到通知?(JavaFX: How to be notified when a Property is no longer reachable?)

系统教程 行业动态 更新时间:2024-06-14 16:59:46
JavaFX:如何在无法再访问属性时收到通知?(JavaFX: How to be notified when a Property is no longer reachable?)

我在Windows x64上用Clojure(1.6)编写了一堆JavaFX(8.0)代码。

我的域/应用程序数据是嵌套的Clojure哈希映射,它位于ref / atom中,我希望将地图的特定子元素绑定到一个或多个UI元素。 称这个大地图为global-var 。 我不希望将我的域数据保存为一堆JFX属性,因为这是使用Clojure及其数据结构和函数的全部要点,所以我不考虑使用JFX Bind...函数将UI元素Bind...到我的域名数据。 我只处理JFX属性,因为它们自然存在于JFX对象中并且试图不创建更多。

为了实现“可观察”模式,我在每个JFX属性上都有一个ChangeListener (例如TableView TableCell的textProperty ),它更新了整个global-var。

为了通知global-var更改的各种属性,我还对接收更改通知的每个属性的global-var执行add-watch ,以及一些防止无限循环的特殊情况。

问题是没有注册手表。 在TableView的情况下,JFX引擎始终创建新的单元格 - 至少两次用于显示窗口,并且每次更新global-var时显然至少两次。

具体来说,按照我发现的各种示例,当调用列的TableCell的updateItem方法时,我为该单元格创建一个新的图形元素。 这允许我为列中的每一行提供不同的单元格类型,而不是列中每个单元格相同的默认单元格类型。 因此,每次更新单元格时,都会创建一个新的(代理)TableCell,并将一个新的监视添加到global-var。

我不介意不断创建新对象,因为JVM应该善于垃圾收集,但我很生气,因为每个新的UI元素我都在为global-var添加一个额外的监视,没有机制可以知道什么时候remove-watch 。 即使使用单个属性,每次数据更改时手表的数量也会增加(不受限制),因为TableView的工作方式。

有一些特殊情况可能基于Stage的onCloseRequest ,但这些不适用于TableView 。

所以问题是,有没有办法通过某种类型的析构函数,事件,回调或其他通知来查找JFX Property何时不再可访问,这样我可以remove-watch ?

谢谢

I'm writing a bunch of JavaFX (8.0) code in Clojure (1.6) on Windows x64.

My domain/application data is a nested Clojure hash-map that lives in a ref/atom, and I wish to tie specific sub-elements of the map to one or more UI elements. Call this big map global-var. I do not wish to maintain my domain data as a bunch of JFX Properties, since this was the whole point of using Clojure and its data structures and functions, so I'm not considering using JFX Bind... functions to tie UI elements to my domain data. I'm only dealing with JFX Properties as they exist naturally inside JFX objects and trying not to create more.

To implement the "observable" pattern, I have a ChangeListener on each JFX Property (such as the textProperty of a TableCell inside a TableView) which updates the entire global-var.

To notify the various properties of changes to global-var, I also do add-watch on the global-var for each property receiving change notification, with some special cases to prevent infinite looping.

The problem is unregistering watches. In the case of TableView, the JFX engine creates new cells all the time -- at least twice just for showing the window, and evidently at least twice every time I update the global-var.

Specifically, following the various examples I've found, when the column's TableCell's updateItem method is called, I create a new graphic element for that cell. This allows me a different cell type for each row within a column, rather than the default where each cell in a column is the same. So every time the cell is updated, a new (proxy of) TableCell is created, and a new watch is added to the global-var.

I don't mind constantly creating new objects since the JVM is supposed to be good at garbage collection, but I'm annoyed that with each new UI element I'm adding an additional watch to the global-var, with no mechanism to know when to do remove-watch. Even with a single property, the number of watches increments (without bound) each time the data changes, because of how TableView works.

There are some special cases possible based on the Stage's onCloseRequest, but these don't apply in the case of TableView.

So the question is, Is there a way of finding when a JFX Property is no longer accessible, either through some type of destructor, event, callback, or other notification, such that I may remove-watch?

Thanks

最满意答案

细胞被重复使用。 在updateItem ,首先清理旧项目之后,然后将其设置为新项目。 我不知道add-watch / remove-watch在Clojure中是如何工作的,但这里是updateItem方法的框架:

public void updateItem(T item, boolean empty) { super.updateItem(item, empty); removeOldWatch(); // whatever that means addWatch(); // whatever that means }

Cells are reused. In updateItem, first clean up after the old item and then set it up for the new item. I don't know how add-watch/remove-watch works in Clojure, but here is the skeleton for your updateItem method:

public void updateItem(T item, boolean empty) { super.updateItem(item, empty); removeOldWatch(); // whatever that means addWatch(); // whatever that means }

更多推荐

本文发布于:2023-04-17 08:51:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/b7283a58381dc5ea6c39739dac1055d0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   通知   如何在   JavaFX   reachable

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!