JavaFX 属性删除侦听器不起作用

Posted

技术标签:

【中文标题】JavaFX 属性删除侦听器不起作用【英文标题】:JavaFX property remove listener not working 【发布时间】:2016-10-10 21:52:12 【问题描述】:

我尝试向 JavaFX BooleanProperty 添加、删除并再次添加一个侦听器,但它不起作用。

这是我的代码

public class PropListenerTest 

    BooleanProperty test = new SimpleBooleanProperty(false);

    public PropListenerTest() 
        System.out.println("\nTest 1\tadd the listener"); //NON-NLS
        test.addListener(this::onChangeTest);
        test.set(true);
        test.set(false);

        System.out.println("\nTest 2\tremove the listener, but not possible! Why?"); //NON-NLS
        test.removeListener(this::onChangeTest);
        test.set(true);
        test.set(false);

        System.out.println("\nTest 3\tAdd the listener again, but now i have two listener but I want only one!"); //NON-NLS
        test.addListener(this::onChangeTest);
        test.set(true);
        test.set(false);
    

    private void onChangeTest(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean newValue) 
        System.out.println("observable = [" + observable + "], oldValue = [" + oldValue + "], newValue = [" + newValue + "]"); //NON-NLS
    

    public static void main(String[] args) 
        new PropListenerTest();
    

结果如下

Test 1  add the listener
observable = [BooleanProperty [value: true]], oldValue = [false], newValue = [true]
observable = [BooleanProperty [value: false]], oldValue = [true], newValue = [false]

Test 2  remove the listener, but not possible! Why?
observable = [BooleanProperty [value: true]], oldValue = [false], newValue = [true]
observable = [BooleanProperty [value: false]], oldValue = [true], newValue = [false]

Test 3  Add the listener again, but now i have two listener but want only one
observable = [BooleanProperty [value: true]], oldValue = [false], newValue = [true]
observable = [BooleanProperty [value: true]], oldValue = [false], newValue = [true]
observable = [BooleanProperty [value: false]], oldValue = [true], newValue = [false]
observable = [BooleanProperty [value: false]], oldValue = [true], newValue = [false]

我认为在测试 2 中应该没有结果,在测试 3 中应该显示与测试 1 相同的结果。 我不知道我做错了什么。有人可以帮我吗?

谢谢

【问题讨论】:

【参考方案1】:

方法引用的行为就像每次都创建一个不同的对象一样。

想象一下

ChangeListener<Boolean> changeListener1 = new ChangeListener() 
    @Override
    public void changed(Observable<? extends Boolean> obs, Boolean oldValue, Boolean newValue)  
;

ChangeListener<Boolean> changeListener2 = new ChangeListener() 
    @Override
    public void changed(Observable<? extends Boolean> obs, Boolean oldValue, Boolean newValue)  
;

那么changeListener1 == changeListener2changeListener1.equals(changeListener2) 将是错误的。

同样,

ChangeListener<Boolean> changeListener1 = this::onChangeTest ;
ChangeListener<Boolean> changeListener2 = this::onChangeTest ;

也会导致changeListener1 == changeListener2 为假。

如果你这样做

ChangeListener<Boolean> changeListener = this::onChangeTest ;

System.out.println("\nTest 1\tadd the listener"); //NON-NLS
test.addListener(changeListener);
test.set(true);
test.set(false);

System.out.println("\nTest 2\tremove the listener, but not possible! Why?"); //NON-NLS
test.removeListener(changeListener);
test.set(true);
test.set(false);

System.out.println("\nTest 3\tAdd the listener again, but now i have two listener but I want only one!"); //NON-NLS
test.addListener(changeListener);
test.set(true);
test.set(false);

它将按照您的预期运行。

【讨论】:

谢谢!效果很好,我不知道这个对不起。

以上是关于JavaFX 属性删除侦听器不起作用的主要内容,如果未能解决你的问题,请参考以下文章

为啥 addEventListener 属性对动态 HTMLcollection 对象不起作用?

React document.removeEventListener 不起作用

为啥禁用舞台可调整大小在 javafx 中不起作用?

JavaFX 8:javafx.fxml.LoadException - fxmlLoader.load() 不起作用,但 fxmlLoader.getController() 起作用

OSX 系统菜单栏在 JavaFX 中不起作用

调用事件侦听器的 HTML 按钮不起作用 HTML 和 JS