3
我嘗試添加,刪除並再次將偵聽器添加到JavaFx BooleanProperty
但它無法工作。JavaFX屬性刪除偵聽器不工作
這裏是我的代碼
公共類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中相同的結果。 我不知道我在做什麼錯,有人可以幫我嗎?
謝謝
謝謝!很好,我不知道這個抱歉。 –