java - 实现弱监听器时,监听器添加到什么源类型? ( java )

标签 java listener netbeans-platform

关于this关于 WeakListeners 的文档我发现代码被剪掉了:

    private void registerTo(Source source) {
        listener = new Listener();
        source.addChangeListener(WeakListeners.change (listener, source));
    }

    private class Listener implements ChangeListener {
        public void stateChanged(ChangeEvent e) {
            doSomething();
        }

他们正在写的那个源对象是什么?我们在谷歌上找不到任何东西。

最佳答案

我检查了您链接的文档。 再往下说:

It is itself strongly referenced from the implementation of the source (e.g. from its EventListenerList)

给你一个可以在那里使用的类的例子。 确实在文档中

WeakListeners.change (listener, source)

源是一个对象,因此它可以是您用作事件源的任何内容。 如果您继续阅读该文档,您会看到:

It tries to unregister itself from the source. This is why it needs the reference to the source for the registration. The unregistration is done using reflection, usually looking up the method remove of the source and calling it.

还有

This may fail if the source don't have the expected remove* method and/or if you provide wrong reference to source. In that case the weak listener instance will stay in memory and registered by the source, while the listener and observer will be freed.

所以我想您将需要使用带有某种删除方法的接口(interface)。

我浏览了 Javadoc 索引,发现这个 ( MenuBar ) 作为我认为您正在寻找的示例。

希望这有帮助。

干杯!

关于java - 实现弱监听器时,监听器添加到什么源类型? ( java ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58911352/

相关文章:

java - 如何知道一个分离的 JPA 实体是否已经被持久化?

java - 使用 IN 子句更新 JPQL 中的用户实体字段

java - JSF 2 - 如何将 Ajax 监听器方法添加到复合组件接口(interface)?

logging - 在Powershell脚本中使用滚动日志文件收集性能计数器

java - 功能测试 (Jellytools) 不在 netbeans 平台上启动

java - 断言-内部调用的问题

java - 如何使用流、Map<String, Object> 按包含键的 List<String> 进行过滤

java - 为什么这个弱监听器没有被垃圾收集?

java - 在 Netbeans 平台应用程序中使用 JavaFX

java - 如何在拖放过程中显示图标