2016-06-10 67 views
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中相同的結果。 我不知道我在做什麼錯,有人可以幫我嗎?

謝謝

回答

5

方法引用將像每次創建不同的對象一樣工作。

想象做

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

它會表現得如您所願。

+0

謝謝!很好,我不知道這個抱歉。 –