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

时间:2016-06-10 03:52:24

标签: javafx properties listener

我尝试添加,删除并再次向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 个答案:

答案 0 :(得分:7)

方法引用将表现为每次都创建一个不同的对象。

想象一下

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);

它会表现得像你期望的那样。